简体   繁体   English

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

[英]How to hide 'Back' button on navigation bar on iPhone?

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?关于如何隐藏后退按钮的任何想法?

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

Swift: Swift:
navigationItem.hidesBackButton = true

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;

hide back button with bellow code...使用以下代码隐藏后退按钮...

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

or或者

[self.navigationItem setHidesBackButton:YES];

Also if you have custom UINavigationBar then try bellow code另外,如果您有自定义UINavigationBar ,请尝试以下代码

self.navigationItem.leftBarButtonItem = nil;

In Swift :Swift 中

Add this to the controller将此添加到controller

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

Use the code:使用代码:

 self.navigationItem.backBarButtonItem=nil;

In the function viewDidLoad of the UIViewController use the code:在 UIViewController 的 function viewDidLoad 中使用代码:

self.navigationItem.hidesBackButton = YES;

Don't forget that you need to call it on the object that has the nav controller.不要忘记您需要在具有导航 controller 的 object 上调用它。 For instance, if you have nav controller pushing on a tab bar controller with a RootViewController, calling self.navigationItem.hidesBackButton = YES on the RootViewController will do nothing.例如,如果您有 nav controller 使用 RootViewController 按下选项卡栏 controller,则在 RootViewController 上调用self.navigationItem.hidesBackButton = YES将无济于事。 You would actually have to call self.tabBarController.navigationItem.hidesBackButton = YES您实际上必须调用self.tabBarController.navigationItem.hidesBackButton = YES

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

Add this code in your view controller在您的视图中添加此代码 controller

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

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.我正在使用带有“嵌入”在导航 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 项时,给它一个至少包含一个字符的标题,在我的例子中是一个空格字符。

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

It wasn't working for me in all cases when I set当我设置时,它在所有情况下都不适用于我

self.navigationItem.hidesBackButton = YES; self.navigationItem.hidesBackButton = 是;

in viewWillAppear or ViewDidLoad, but worked perfectly when I set it in init of the viewController.在 viewWillAppear 或 ViewDidLoad 中,但是当我在 viewController 的 init 中设置它时效果很好。

try this one - self.navigationController?.navigationItem.hidesBackButton = true试试这个 - self.navigationController?.navigationItem.hidesBackButton = true

In c# or Xamarin.ios, this.NavigationItem.HidesBackButton = true;在 c# 或 Xamarin.ios 中,this.NavigationItem.HidesBackButton = true;

navigationItem.leftBarButtonItem = nil
navigationItem.hidesBackButton = true

if you use this code block inside didLoad or loadView worked but not worked perfectly.İf you look carefully you can see back button is hiding when your view load.Look's weird.如果您在 didLoad 或 loadView 中使用此代码块,但可以正常工作但不能正常工作。如果您仔细看,您可以看到后退按钮在您的视图加载时隐藏。看起来很奇怪。

What is the perfect solution?什么是完美的解决方案?

Add BarButtonItem component from componentView (Command + Shift + L) to your target viewControllers navigation bar.将来自 componentView (Command + Shift + L)BarButtonItem组件添加到目标 viewControllers 导航栏。

Select BarButtonItem set Title = " " from right panel Select BarButtonItem 设置 Title = " " 从右侧面板

在此处输入图像描述

self.navigationItem.setHidesBackButton(true, animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM