iOS 8: UINavigationController hide back button

2020-06-07 08:05发布

After I run my application in iOS 8 (XCode 6.0.1, iPhone 6), the back button does not hide.

My code:

- (void)removeCategoriesButton
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [_navigationController.topViewController.navigationItem setHidesBackButton:YES];
        [_navigationController.topViewController.navigationItem setLeftBarButtonItem:nil];
    } else {
        UIViewController *controller = _app.window.rootViewController;

        if ([controller isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nav = (UINavigationController *)controller;
            [nav.topViewController.navigationItem setHidesBackButton:YES];
            [nav.topViewController.navigationItem setLeftBarButtonItem:nil];
        }
    }
}

But the back button does not hide (see screenshot):

Simulator screen

UPD:

I run application in another simulators, and i see this "bug" only on iOS 8.

11条回答
forever°为你锁心
2楼-- · 2020-06-07 08:33

I tried many of the answers but the only one that worked for me was:

    override func viewDidLoad() {
    super.viewDidLoad()

    let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
    navigationItem.leftBarButtonItem = backButton
}
查看更多
劳资没心,怎么记你
3楼-- · 2020-06-07 08:33

This bug only happens when you use Storyboard. Another solution is add an UIBarButtonItem with empty title to "fake" it.

查看更多
在下西门庆
4楼-- · 2020-06-07 08:42

Call on your ViewDidLoad the following method:

Objective-C:

self.navigationItem.leftBarButtonItem = nil;

or

self.navigationItem.hidesBackButton = YES;

Swift:

navigationItem.hidesBackButton = true
查看更多
Juvenile、少年°
5楼-- · 2020-06-07 08:42

Try to use self.navigationItem.hidesBackButton = true in viewWillAppear() method, this worked for me.

查看更多
Explosion°爆炸
6楼-- · 2020-06-07 08:43

Where have you written that code?

It should be as simple as in your view controller's loadView/viewDidLoad: method adding this

[self.navigationItem setHidesBackButton:YES];

This works for me on an iPhone 6

查看更多
再贱就再见
7楼-- · 2020-06-07 08:43

The only way I've found to do this is to hide the navigation bar and adding a navigation bar in storyboard and redisplay the navigation bar in the next ViewController. All I had to do is add a label in the status bar so that the navigation bar is uniform. I have found no other way...

screen

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[self navigationController] setNavigationBarHidden:YES animated:YES];
}

so that the navigation bar is displayed in the next viewcontroller, declare in:

- (void)viewWillDisappear:(BOOL)animated
{
    [[self navigationController] setNavigationBarHidden:NO animated:YES];

}
查看更多
登录 后发表回答