Smooth transition when changing navigation bar "prefersLargeTitles" - ios

I have a view controller that is pushed onto a navigation stack. The stack has navigationBar.prefersLargeTitles = true, whilst this new view controller has navigationBar.prefersLargeTitles = false. I achieve this using the following code in the view controller that is pushed onto the stack:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
}
However, when I return back to the presenting view controller, the change in the navigation bar from navigationBar.prefersLargeTitles = false to navigationBar.prefersLargeTitles = true is a bit glitchy. Is there any way to make this smoother?
Many thanks

Instead of directly changing the preference via the navigation controller, you should change the behavior via the navigation item of the specific view controller you would like to affect.
// Root UIViewController
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
}
}
// Pushed UIViewController
class ViewControllerB: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.largeTitleDisplayMode = .never
}
}
You can remove the lines you have in viewWillAppear and viewWillDisappear.

Related

hidesBottomBarWhenPushed hides TabBar forever

I use Xcode 11.2 and the project minimum iOS deployment target is iOS 12.4.
I have a TabBarController on root page and on one of the tabs I have FirstViewController. When I push SecondViewController from FirstViewController, I want the tab bar to be hidden. I used hidesBottomBarWhenPushed property to hide the tab bar.
The tab bar is hidden when I push SecondViewController but when I pop the SecondViewController and move back to FirstViewController, the tab bar is still hidden.
I tried several ways to set hidesBottomBarWhenPushed to false when moving back to FirstViewController but none of the tries worked.
How can I re display tab bar when popped back to FirstViewController?
class FirstViewController: UIViewController {
#IBAction func buttonTap(_ sender: Any) {
let vc2 = SecondViewController()
// Set to Hide TabBar
hidesBottomBarWhenPushed = true
navigationController?.pushViewController(vc2, animated: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// This Does Not Work
hidesBottomBarWhenPushed = false
}
}
class SecondViewController: UIViewController {
/*
All The Followings Does Not Work
*/
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
hidesBottomBarWhenPushed = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
hidesBottomBarWhenPushed = false
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
hidesBottomBarWhenPushed = false
}
}
The key was to set hidesBottomBarWhenPushed to true from outside of the SecondViewController.
The code below was all I needed to write.
class FirstViewController {
func pushSecondViewController {
let vc = SecondViewController()
vc.hidesBottomBarWhenPushed = true // <- Here
navigationController?.push
navigationController?.pushViewController(vc, animated: true)
}
}

How to keep navigation bar title large after pushing view controller from TableView

I have a tableview displaying on a main view controller, and when a row is selected a detail view controller is pushed. I have a large title for the main view controller, and a small/regular title for the detail view controller. They are embedded in a navigation controller and tab bar controller.
Before selecting a row, the main view controller title is large, and when a row is selected the detail view controller title is regular as it should be. However when I return to the main view controller from the detail view controller (via "back" button), the title on the main view controller is no longer large.
I have "prefersLargeTitles" set to true on the main view controller, and "largeTitleDisplayMode" set to never on the detail view controller.
I have tried setting "largeTitleDisplayMode" to always on the main view controller to no avail. I've also tried setting it to automatic on either view controller and seems to have no effect.
I've also tried using "viewWillAppear" and "viewWillDisappear" and setting the title in there, and while it does indeed reset the main view controller title back to large, the animation lags and isn't smooth like it normally is when transitioning from a small title to a large title.
Also I'm pretty new to coding and this is my first app I'm building without using storyboards, so the code could be a mess.
MAIN view controller code:
class HomeViewController: UIViewController {
let tableView = UITableView()
override func loadView() {
super.loadView()
view.backgroundColor = .white
self.title = "Home"
// Set large title
navigationController?.navigationBar.prefersLargeTitles = true
// Make navigation bar transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
extension HomeViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = DetailViewController() as DetailViewController
if indexPath.section == 0 {
vc.detailTitle = itemsTop[indexPath.row]
} else if indexPath.section == 1 {
vc.detailTitle = itemsBottom[indexPath.row]
} else {
print("Failed to load title")
}
navigationController?.pushViewController(vc, animated: true)
tableView.deselectRow(at: indexPath, animated: true)
}
}
DETAIL view controller code:
class DetailViewController: UIViewController {
var detailTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
title = detailTitle
// Make nav bar transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
// Prevent large title
navigationController?.navigationBar.prefersLargeTitles = false
}
}
try this it's working.
In 'HomeViewController'
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
}
In 'DetailViewController'
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.largeTitleDisplayMode = .never
}
You need to set navigationController?.navigationBar.prefersLargeTitles to true in viewWillAppear of your main view controller to let it run every time you go back to main.
You can set it to false in viewWillAppear of detail and revert it in viewWillDisappear of detail to prevent it to remain as false as well.
The code for your DetailVC:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
}

Swift 4 One View Controller two views hide navigation bar

I have one View Controller and this View Controller contains two views/scenes in the main.storybard.
I am trying to hide the top navigation bar at first view/scene, but unhide it again on the second view/scene.
I tried with
self.navigationController?.isNavigationBarHidden = true
But this will only work with two View Controller classes.
Does anyone have a idea to manage it?
Hide navigationbar in viewWillAppear & unhide in viewWillDisappear
var shouldHideNavBar = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(shouldHideNavBar, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if shouldHideNavBar == true {
navigationController?.setNavigationBarHidden(false, animated: animated)
}
}
And when you perform segue set shouldHideNavBar as true
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "show") {
let viewController = segue!.destinationViewController as! ViewController
viewController.shouldHideNavBar = true
}
}
You should use extra control. Or you can create an IBInspactable variable and assing it's value on Interface Builder. Like this:
#IBDesignable class myViewController: UIViewController{
#IBInspectable var isNavbarHidden: Bool = true{
didSet{
self.navigationController?.isNavigationBarHidden = isNavBarHidden
}
}
override func viewDidLoad(){
super.viewDidLoad()
//I am not sure if this line is necessary
self.navigationController?.isNavigationBarHidden = isNavBarHidden
}
}
After then go to InterfaceBuilder(your storyboard file) and set its value for your Scenes on your viewControllers properties.

Swift Navigation Bar Color

Is it possible to set the navigation bar color for just a single View Controller in the navigation hierarchy? Let the default navigation bar color be red and just the last view controller in the line should have a blue one. I've used these two lines to color the navigation bar of said view controller:
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.tintColor = .white
But when going back (e.g. by pressing the back button) the navigation bar stays blue. Setting the color back to red using above code doesn't do anything.
The navigationBar is shared across all the view controllers that are in the same UINavigationController stack.
If you want to change it's look for a specific view controller, you'll have to set the new style when the view controller is shown and remove it when the view controller is dismissed. This could be done in the viewWillAppear/viewWillDisappear of your view controller for example.
I could get the navigation bar to change colors coming from a ViewControllerB to ViewControllerA perfectly fine with your code. I am not sure what your initial problem was. Here is my code which worked:
ViewController A:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonAction(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Second")
//self.present(controller, animated: true, completion: nil)
self.navigationController?.pushViewController(controller, animated: true)
}
}
ViewController B:
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .blue
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
It worked without an issue.

How can I hide the tab bar for a single view controller in a navigation controller stack

I have a UITabBarController. One of the tabs contains a UINavigationController.
I'd like to push a view controller onto the navigation stack and hide the tab bar on that view controller. I can do this easily with:
toVC.tabBarController?.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(toVC, animated: true)
or doing it in the storyboard:
The problem is, this hides the tab bar for any subsequent view controllers I push onto the stack. I'd like to simply hide the tab bar for this one view controller and show it for all other view controllers before and after it.
There is a workaround. It works the way it is presented on gif below.
For each UIViewController that is pushed into the UINavigationController stack I override the hidesBottomBarWhenPushed property this way:
override var hidesBottomBarWhenPushed: Bool {
get {
switch navigationController?.topViewController {
case .some(let controller):
switch controller == self {
case true:
return super.hidesBottomBarWhenPushed
case false:
return false
}
default:
return super.hidesBottomBarWhenPushed
}
}
set {
super.hidesBottomBarWhenPushed = newValue
}
}
The first switch checks whether this controller belongs to some UINavigationController stack. The second switch checks whether current top UIViewController of UINavigationController stack is self.
Hope it will work in your case. Happy coding (^
If you hide on the storyboard then by this property your tab bar will hide for all the view controllers. So you can manage this by code.
You can do this programmatically by just writing one line of code in ViewDidLoad() or ViewWillAppear() method
For Swift 3:-
self.tabBarController?.tabBar.isHidden = true
And where you want to unhide the tab bar just write the following code in ViewDidLoad () or ViewWillAppear() method
self.tabBarController?.tabBar.isHidden = false
Try this in the view controller you want to hide the tab bar in:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
}
And this in the view controllers before and after the one you want to hide the tab bar in:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
EDIT:
Fully implemented example:
class ViewController1: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
}
class ViewController2: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
}
}
class ViewController3: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
}

Resources