开发者

Hide - Unhide backBarButton of UINavigationBar?

开发者 https://www.devze.com 2023-03-09 08:13 出处:网络
I want to do something like this.All controllers are pushed on navigation controller. ViewController 1 -> ViewController 2(Do not show back button which appears automatically by pushing) -> ViewContr

I want to do something like this.All controllers are pushed on navigation controller. ViewController 1 -> ViewController 2(Do not show back button which appears automatically by pushing) -> ViewController 3(Show Back button of navigation bar) -> ViewController 4(Again hide back Bar button).

I've tried setting title to empty for ViewController1 but then back button does not appear anywhere in the app. Also I've tried setting back bar button to nil before pushing ViewController2 on navigation stack. But nothing works as expected. Do开发者_如何学Ces someone knows how to do this. Note - I want default back bar button which appears when we push a viewController on stack not the left bar button item. If no option is left then i'll prefer left bar button item approach.


I have done something similar to this before. In the controller that's going to push the next view controller that you don't want a back button in put this wherever you're doing your pushViewController:

myNextViewController.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:myNextViewController animated:YES];

In your case this would be in the 1st and 3rd viewControllers in the stack.


The hidesBackButton approach shows a animation of the back bar button being pushed out. However small, like you said this doesn't seem right. The dirty option forward would be to do this in the parent view controller.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = @"Title";
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.title = @"";
}

Original Answer

You will need to add this to the view controllers where you want to hide the back button. Pretty self explanatory.

self.navigationItem.hidesBackButton = YES;
0

精彩评论

暂无评论...
验证码 换一张
取 消