Thursday 8 December 2016

Manage navigation controller

In iOS app development UINavigationController play a vital role . As we all know UINavigationController keeps the stack of view controller when we use in our app for transition among view controllers .

We face many problems related to UINavigationController like change appearance of the navigation bar, add UIBarButton items to navigation bar , combine the status bar with navigation bar and some others. All these tasks seem very easy at first look but it depends on our requirements .

In my recent app I want to use the navigation bar with two buttons (left and right bar button item)in all view controller and also want the color of status bar and navigation bar should be same . 

1) To change the appearance of the UINaviagtionBar, we can code in our AppDelegate this will reflect in all view controllers: 

    var navBarAppearance = UINavigationBar.appearance()

    navBarAppearance.tintColor = UIColor.white

    navBarAppearance.barTintColor  = UIColor.init(red: 73/255.0, green: 180/255.0, blue: 255/255.0, alpha: 1)

 a) Here tintColor attribute will change the background color of the navigation bar .
 b) Bar tint color will change the color of button titles and  button images.

The above two properties won't change the color of the title of the navigation bar. To change the color of the title use this line of code:
   
   navBarAppearance.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]


 2) Change the status bar color:

  UIApplication.shared.statusBarStyle = .lightContent

This will not change the color of the status bar. To reflect the change we need to make one more change in our info.plist file:
set UIViewControllerBasedAppearance to false

3) Add bar button items to all view controller: This was my requirement to have two buttons in navigation bar in all view controllers: I can not achieve this like appearance. Because bar buttons are added to navigation item property of the view controller. Each view controller have its own navigation item . 
So to add bar  buttons to all view controller:

a) Add in viewDidLoad() of each view controller
b) Make a Utility class and add a class function to it and call this from each view controller's viewDidLoad() or any other methods of view life cycle as per your need .
c) Make an separate subclass of UIViewController and add two bar buttons here in this class and change the super class of your view controller from UIViewController to one you created previous .

I will  choose the last approach. Add this code in your subclass:
     let leftBtn = UIBarButtonItem(image: UIImage(named:"back_btn_icon"), style: .plain, target: self, action: #selector(backBtnTapped))
   self.navigationItem.leftBarButtonItem = leftBtn

    let rightBtn = UIBarButtonItem(image: UIImage(named:"setting_icon"), style: .plain, target: self, action: #selector(settingBtnTapped))

    self.navigationItem.rightBarButtonItem = rightBtn

What if one of your view controller need only one bar button. It's easy we can hide the unwanted button:
 To hide the left button add this line your view controller:

self.navigationItem.leftBarButtonItems?.remove(at: 0)



Reference: Stackoverflow and https://coderwall.com



1 comment: