如何隐藏UINavigationController中的“后退”button?

你知道如何隐藏UINavigationController中的“后退”button吗? 另外,如何显示它,但我想这是非常相似的隐藏它…

就像邮件应用程序在iPhone上查看电子邮件时点击“编辑”一样。

我只是find答案,在一个控制器使用这个:

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

并恢复它:

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

添加此代码

 [self.navigationItem setHidesBackButton:YES]; 

除了删除后退button(使用已经推荐的方法)之外,不要忘记用户仍然可以在iOS 7及更高版本中以“从左到右”的轻扫手势“popup”到前一个屏幕。

要禁用(如果适用),请执行以下操作(例如,在viewDidLoad中):

  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) self.navigationController.interactivePopGestureRecognizer.enabled = NO; 

要有条件地隐藏和显示“后退”button,可以使用以下代码:

 -(void)viewDidAppear:(BOOL)animated { if ([tempAry count]==0) { [self.navigationItem setHidesBackButton:YES animated:YES]; } else { [self.navigationItem setHidesBackButton:NO animated:YES]; } [super viewDidAppear:animated]; } 

注意:在某些情况下,你必须把它放在viewDidAppear方法中而不是viewWillAppear这样的情况下,例如:当你将下一个类的数组更新到前一个类中,然后检查条件到上一个类中时。

只是为了澄清现有的答案: hidesBackButton属性是正确的答案,但在许多答案中不清楚self指的是什么。 基本上你应该设置self.navigationItem.hidesBackButton = YES在视图控制器即将推(或刚刚推到)到UINavigationController

换句话说,我说我有一个名为myNavControllerUINavigationController 。 我想提出一个新的观点,当我这样做,我不希望后退button显示了。 我可以做这样的事情:

 UIViewController *newVC = [[UIViewController alloc] init]; //presumably would do some stuff here to set up the new view controller newVC.navigationItem.hidesBackButton = YES; [myNavController pushViewController:newVC animated:YES]; 

代码完成后,现在应该显示由newVC控制的视图,并且不应该显示后退button。

由于某种原因sethidesbackbutton没有为我工作

我用这种方式 – >

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

Swift iOS(我已经使用过了)

 // hide back button self.navigationItem.setHidesBackButton(true, animated: false) // pgrm mark ----- ------ // hide the back button for this view controller override func setEditing(editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) self.navigationItem.setHidesBackButton(editing, animated: animated) }// end setEditing 

在我的情况下,我目前的答案几乎没有问题:

  • 里面viewDidLoad / viewWillAppear只有后面的图标被隐藏,string“后退”是无效的,但仍然可见
  • 里面viewDidAppear后退button消失…但我不希望用户看到它

所以终于为我工作的解决scheme是:

 - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self.navigationItem setHidesBackButton:YES animated:NO]; } return self; } 

总是使用Apple文档来解决简单问题,他们更直接,更轻量级:)

这里是Swift 3.0的语法:

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

参考

https://developer.apple.com/reference/uikit/uinavigationitem#//apple_ref/occ/instm/UINavigationItem/setHidesBackButton:animated:;

Zoran Simic提出的解决scheme由于某种原因对我不起作用。

但是这个代码确实有效:

 MyController* controller = [[MyController alloc] init]; NSArray* array = [[[NSArray alloc] initWithObjects:controller, nil] autorelease]; [self.navigationController setViewControllers:array animated:NO]; [controller release]; 

显然,你必须操纵一个NSArray,以适应你的口味。 希望有助于某人:)

在我的UIViewController子类中,我有这个方法:

 -(void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated: animated]; // hide back button in edit mode [self.navigationItem setHidesBackButton:editing animated:YES]; } 

这隐藏了后退button,并用Swift中的添加buttonreplace它:

 override func setEditing(_ editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) // This hides the back button while in editing mode, which makes room for an add item button self.navigationItem.setHidesBackButton(editing, animated: animated) if editing { // This adds the add item button let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped)) // Use the animated setter for the left button so that add button fades in while the back button fades out self.navigationItem.setLeftBarButton(addButton, animated: animated) self.enableBackGesture(enabled: false) } else { // This removes the add item button self.navigationItem.setLeftBarButton(nil, animated: animated) self.enableBackGesture(enabled: true) } } func enableBackGesture(enabled: Bool) { // In addition to removing the back button and adding the add item button while in edit mode, the user can still exit to the previous screen with a left-to-right swipe gesture in iOS 7 and later. This code disables this action while in edit mode. if let navigationController = self.navigationController { if let interactivePopGestureRecognizer = navigationController.interactivePopGestureRecognizer { interactivePopGestureRecognizer.isEnabled = enabled } } } 

Swift 3。

一般来说,您应该使用Apple的per-ViewController API,如本页面上已经描述的那样,但是有时您需要立即控制Backbutton。

以下代码隐藏“后退”button,并确保在隐藏的button区域中不会出现敲击碰撞检测。

 let emptyView = UIView(frame: .zero) self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: emptyView) 

这隐藏了后退button

 let backBtn = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil) navigationItem.leftBarButtonItem = backBtn