Custom back button for NavigationController for each screen

I am trying to subclass the UINavigationController class to change things like background image, navigation bar size, back button, font font, etc. After a couple of hours and a lot of reading, I realized that I should not subclass the UINavigationController class ...

I would like to have a specific design and transfer this design to all screens that use my custom navigation controller ...

So far, I do not have any elegant solution for this, most solutions are based on setting something to show only on the next screen (next view).

How can i achieve this? Maybe using categories? Thanks in advance.

+1
source share
4 answers

I am showing the code for the custom left pane of the UINavigationcontroller . You can add this code to each view controller and for both buttons (left and right)

  leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [leftButton setImage:[UIImage imageNamed:@"Yourcutomeimage.png"] forState:UIControlStateNormal]; leftButton.frame = CGRectMake(0, 0, 30, 30); [leftButton addTarget:self action:@selector(youraction:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton]autorelease]; 

//or

 self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton]autorelease]; 

Hope this helps you.

+7
source

Yes @Nit is correct, this is a way to configure UINavigationController , but if you do not want to do this for each UIViewController , you can create a UIViewControllerBase class, which is a subclass of UIViewController and in the viewDidLoad method, set all your buttons as described by @Nit, and then do every UIViewController , you must be a subclass of your UIViewControllerBase class.

+1
source

If you want to add a custom back button to the uinavigationcontroller, you must use navigationItem.backBarButtonItem and implement it in the class in which you initialize the uinavigationcontroller. Example: -

 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnImage.png" style:UIBarButtonItemStyleBordered target:youtarget action:@sel(your action)]; self.navigationItem.backBarButtonItem = backButton; [backButton release]; 
0
source

I set out my solution in this answer: fooobar.com/questions/300099 / ...

Basically, you create a category in the UIViewController and call one single method in each viewWillAppear method of your view controllers.

0
source

All Articles