I’m becoming confused these days using UINavigationController;
I’d like to customize the UINavigationController,changing its background image and adding buttons;
I tried this in 2 projects. In the first I did this by using
self.navigationController.navigationItem.leftBarButtonItem = customItem;
but in the secoenter code herend this method doesn’t work.I have to use
self.navigationItem.leftBarButtonItem = customItem;
if a ViewController is a rootViewController of a NavigationController,it has two properties:
self.navigationController
self.navigationItem
and the Navigation has 3 properties:
self.navigationController.navigationController
self.navigationController.navigationBar
self.navigationController.navigationItem
why the NavigationController also has a property NavigationController?
why sometimes I use
self.navigationController.navigationItem
but sometimes I have to use
self.navigationItem
I’m really confused.
Because it inherits from
UIViewController, which has this property.As a general rule, you don’t want to directly manipulate the properties of the navigation controller enclosing your view controller (these properties include the navigation controller’s navigation item and navigation bar). Modify
self.navigationItem.whateverPropertyYouWannaSet; the properties of the navigation item are read by the navigation controller upon pushing your view controller onto its stack, and it will setup its own navigation bar according to these properties.