iOS 8:UINavigationController 隐藏后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25994219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:43:28  来源:igfitidea点击:

iOS 8: UINavigationController hide back button

iosobjective-cios8back-buttoniphone-6

提问by ZhukV

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

在 iOS 8(XCode 6.0.1、iPhone 6)中运行我的应用程序后,后退按钮不会隐藏。

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.

我在另一个模拟器中运行应用程序,我只在 iOS 8 上看到这个“错误”。

回答by scrainie

This worked for me.

这对我有用。

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setTitle:@"Home"];
}

回答by eric f.

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
}

回答by LS_

Call on your ViewDidLoad the following method:

在您的 ViewDidLoad 上调用以下方法:

Objective-C:

目标-C:

self.navigationItem.leftBarButtonItem = nil;

or

或者

self.navigationItem.hidesBackButton = YES;

Swift:

迅速:

navigationItem.hidesBackButton = true

回答by William Hu

Swift:

迅速:

self.navigationItem.hidesBackButton = true

回答by Glen Van Der Poel

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.

我发现这是由于在 viewWillAppear 中推送新视图引起的,如果我将其移动到 viewDidAppear 则后退按钮没有显示。奇怪的是这个问题只出现在iOS8。

回答by Chun-Wei Chen

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];
    }
}

回答by Array

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

尝试self.navigationItem.hidesBackButton = trueviewWillAppear()方法中使用,这对我有用。

回答by SomeGuy

Where have you written that code?

你在哪里写的那个代码?

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

它应该像在您的视图控制器的 loadView/viewDidLoad: 方法中添加这个一样简单

[self.navigationItem setHidesBackButton:YES];

This works for me on an iPhone 6

这在 iPhone 6 上对我有用

回答by Nico teWinkel

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

使用 setHidesBackButton 隐藏后退按钮仅在您没有自定义按钮时才有效。

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)

来自方法参考:“如果此导航项是顶部项目时后退按钮应隐藏,则指定 true。如果后退按钮应可见,则指定 false,假设它尚未被自定义项目替换。” (注意最后一行)

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

在这种情况下,简单的解决方案是首先将 leftBarButtonItem 设置为 nil。

Swift 3.0:

斯威夫特 3.0:

self.navigationItem.leftBarButtonItem = nil
self.navigationItem.setHidesBackButton(true, animated: false)

回答by Yue-Hsun Lin

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

此错误仅在您使用 Storyboard 时发生。另一种解决方案是添加一个带有空标题的 UIBarButtonItem 来“伪造”它。