ios 如何隐藏 iPhone 导航栏上的“返回”按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/614212/
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-30 15:43:45  来源:igfitidea点击:

How to hide 'Back' button on navigation bar on iPhone?

iosobjective-ciphonenavigationuinavigationcontroller

提问by Chilly Zhong

I added a navigation control to switch between views in my app. But some of the views shouldn't have 'Back' (the previous title) button. Any ideas about how to hide the back button?

我添加了一个导航控件来在我的应用程序中的视图之间切换。但是有些视图不应该有“返回”(上一个标题)按钮。关于如何隐藏后退按钮的任何想法?

回答by user8170

Objective-C:
self.navigationItem.hidesBackButton = YES;

目标-C:
self.navigationItem.hidesBackButton = YES;

Swift:
navigationItem.hidesBackButton = true

迅速:
navigationItem.hidesBackButton = true

回答by Skrew

The best way is to combine these, so it will hide the back button even if you set it up manually :

最好的方法是将这些组合起来,因此即使您手动设置它也会隐藏后退按钮:

self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;

回答by Paras Joshi

hide back button with bellow code...

用波纹管代码隐藏后退按钮...

[self.navigationItem setHidesBackButton:YES animated:YES];

or

或者

[self.navigationItem setHidesBackButton:YES];

Also if you have custom UINavigationBarthen try bellow code

另外,如果您有自定义,请UINavigationBar尝试以下代码

self.navigationItem.leftBarButtonItem = nil;

回答by King-Wizard

In Swift:

斯威夫特

Add this to the controller

将此添加到控制器

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.setHidesBackButton(true, animated: false)
}

回答by Gurpreet Singh

Use the code:

使用代码:

 self.navigationItem.backBarButtonItem=nil;

回答by indiefr

In the function viewDidLoad of the UIViewController use the code:

在 UIViewController 的函数 viewDidLoad 中使用代码:

self.navigationItem.hidesBackButton = YES;

回答by Bruno Delgado

Don't forget that we have the slide to back gesture now. You probably want to remove this as well. Don't forget to enable it back again if necessary.

别忘了我们现在有滑到后退的手势。您可能也想删除它。如有必要,请不要忘记再次启用它。

if ([self.navigationItem respondsToSelector:@selector(hidesBackButton)]) {
    self.navigationItem.hidesBackButton = YES;
}

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

回答by Tyler

Don't forget that you need to call it on the object that has the nav controller. For instance, if you have nav controller pushing on a tab bar controller with a RootViewController, calling self.navigationItem.hidesBackButton = YESon the RootViewController will do nothing. You would actually have to call self.tabBarController.navigationItem.hidesBackButton = YES

不要忘记您需要在具有导航控制器的对象上调用它。例如,如果您有导航控制器推送带有 RootViewController 的选项卡栏控制器,则调用self.navigationItem.hidesBackButton = YESRootViewController 将不会执行任何操作。你实际上必须打电话self.tabBarController.navigationItem.hidesBackButton = YES

回答by Bhavsar1311

Add this code in your view controller

将此代码添加到您的视图控制器中

UIView *myView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 30)];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc]initWithCustomView:myView];
self.navigationItem.leftBarButtonItem = btnL;

回答by Wayne

For me none of the above seemed to work, It had no visual effect. I am using storyboards with a view that is "embedded" in a navigation controller.

对我来说,以上似乎都不起作用,它没有视觉效果。我正在使用带有“嵌入”在导航控制器中的视图的故事板。

I then at code level add my menuItems and for some reason the "backButton" is visible when visually debugging the view hierarchy, and my menuItem Icon is displayed beneath the invisible "back button".

然后我在代码级别添加我的 menuItems 并且出于某种原因在可视化调试视图层次结构时“backButton”是可见的,并且我的 menuItem 图标显示在不可见的“后退按钮”下方。

I tried the settings, as suggested at the various hook methods and that had no effect. Then I tried a more brutal approach and iterate over the subview which also had no effect.

我按照各种钩子方法的建议尝试了设置,但没有效果。然后我尝试了一种更残酷的方法并迭代子视图,这也没有效果。

I inspected my icon sizes and appeared to be ok. After referring to he apple Human Interface Guideline I confirmed my Icons are correct. (1 pixel smaller in my case 24px 48px 72px).

我检查了我的图标大小,看起来没问题。在参考了苹果人机界面指南后,我确认我的图标是正确的。(在我的情况下小 1 像素 24px 48px 72px)。

The strangest part then is the actual fix...

最奇怪的部分是实际的修复......

When adding the BarButton Item give it a title with at least one character, In my case a space character.

当添加 BarButton Item 时,给它一个至少一个字符的标题,在我的例子中是一个空格字符。

Hopes this helps someone.

希望这有助于某人。

//left menu - the title must have a space
UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " <--THE FIX 
                                                                    style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector(showMenu)];
leftButtonItem.image = [UIImage imageNamed:@"ic_menu"];

[self.navigationItem setLeftBarButtonItem:leftButtonItem];