Omr
Omr

Reputation: 99

Create function to setup navigation controller

How could i create a function that I could use in multiple view controllers that will setup the navigation controller? Ex: In over 15 view controller, I have to put the following code to setup the navigation controller, it is not the ideal solution obviously but I can't figure out how to create a function to reduce the following code into a single function.

self.navigationItem.title = ViewControllerName
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.view.mixedBackgroundColor = StandardViewColor
self.navigationController?.navigationBar.mixedBarTintColor = StandardViewColor
self.navigationController?.navigationBar.mixedTintColor = StandardContrastColor

Thank you in advance!

Upvotes: 0

Views: 24

Answers (1)

Ashik
Ashik

Reputation: 1259

You can create a Base Class (example-name: BaseViewController) which is a subclass of UIViewController, put the the navigation controller codes in its viewDidLoad() method, then make the other 15 ViewControllers sub class of the Base Class.

class BaseViewController: UIViewController{
   void viewDidLoad(){
      //navigationController code goes here
   }
}

class OtherViewController: BaseViewController{}

I hope I'm clear. Feel free to comment.

Upvotes: 1

Related Questions