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条回答
Evening l夕情丶
2楼-- · 2020-06-07 08:49

Swift:

self.navigationItem.hidesBackButton = true
查看更多
等我变得足够好
3楼-- · 2020-06-07 08:52

I found that this was caused by pushing a new view in viewWillAppear, if I moved it to viewDidAppear then the back button didn't show. Strange that this issue only appeared in iOS8.

查看更多
ら.Afraid
4楼-- · 2020-06-07 08:54

Hiding the back button using setHidesBackButton only works if you have not customized the button.

From the method reference: "Specify true if the back button should be hidden when this navigation item is the top item. Specify false if the back button should be visible, assuming it has not been replaced by a custom item." (Note the last line)

The simply solution in that case is to first set the leftBarButtonItem to nil.

Swift 3.0:

self.navigationItem.leftBarButtonItem = nil
self.navigationItem.setHidesBackButton(true, animated: false)
查看更多
Summer. ? 凉城
5楼-- · 2020-06-07 08:55

Try this:

[self.navigationItem setHidesBackButton:YES];

for (UIView *view in self.navigationController.navigationBar.subviews)
{
    NSString *name = [NSString stringWithFormat:@"%@",view.class];
    if ([name isEqualToString:@"UINavigationItemButtonView"] || [name isEqualToString:@"_UINavigationBarBackIndicatorView"]) {
        [view setHidden:YES];
    }
}
查看更多
爷的心禁止访问
6楼-- · 2020-06-07 08:59

This worked for me.

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setTitle:@"Home"];
}
查看更多
登录 后发表回答