Create a custom back button for the UINavigationController

I am developing an application for iOS 4.2+. I have subclassed my UINavigationController to insert two UIImageView and make the navigation bar normal. Everything works fine, but I have a little problem. I created a custom UIBarButtonItem and inside my view managers I put them using

 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton]; 

It works too, but the problem is that to do this work I need to call it from:

  - (void)viewDidAppear:(BOOL)animated ; 

Thus, it appears only after the animation, and I can see the button without settings 1 second before it is replaced.

(I tried with viewWillAppear , but then added nothing to the navigation bar)

I would like to know if you have a solution that could fix this problem.

PS: I never use IB, everything is done programmatically.

Thank you France!

EDIT 1: here is the code that showed nothing for viewWillAppear :

 - (void)viewWillAppear:(BOOL)animated { [self setTitle:@"Jeu"]; //Bouton testflight TIFButton *testFeedbackButton = [[TIFButton alloc]init]; [testFeedbackButton setTitle: @"Envoyer un Feedback" forState:UIControlStateNormal]; [testFeedbackButton addTarget:self action:@selector(launchFeedback) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *testFeedback = [[UIBarButtonItem alloc] initWithCustomView:testFeedbackButton]; self.navigationItem.rightBarButtonItem = testFeedback; TIFBackButton *backButton = [[TIFBackButton alloc]init]; [backButton setTitle: @"Retour" forState:UIControlStateNormal]; [backButton addTarget:self action:@selector(popToPrecedentViewController) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton]; } 
+4
source share
3 answers

Say you have two ViewControllers, A and B, you push B onto the stack when A is the topmost, and you want to customize the back button that appears when B is on top.

Typically, you need to set ViewController A navigationItem.backBarButtonItem .

Instead, you should make ViewController B a custom button on the left side of the navigation bar by setting its navigationItem.leftBarButtonItem .

You have implemented this approach perfectly, except that even if you do not set ViewController A navigationItem.backBarButtonItem , by default you still get the default back button. Thus, the button probably appears on top of your custom back button.

If you set ViewController B navigationItem.hidesBackButton = YES , you should not have any problems.

And in the future, when you implement custom buttons back, you should do this by setting navigationItem.backBarButtonItem instead of navigationItem.leftBarButtonItem . The only thing you need to do is that with this approach, for example, you would use ViewController A navigationItem to change the back button that appears when ViewController B is at the top.

+18
source
 UIBarButtonItem *backButton = [UIBarButtonItem new]; [backButton setTitle:@"Back"]; [[self navigationItem] setBackBarButtonItem:backButton]; 

This code, which you should mention in the previous view controller, then only the default back button appeared in the next view controller.

Enjoy !!!

+3
source

I came up with a somewhat universal solution: fooobar.com/questions/300099 / ... It includes a category on the UIViewController and requires minimal code to enable the image return button on all screens.

+1
source

Source: https://habr.com/ru/post/1396709/


All Articles