你知道如何隐藏UINavigationController中的“后退”按钮吗? 另外,如何展示它,但我想这与隐藏它非常相似......
就像邮件应用程序在查看电子邮件时点击“编辑”一样在iPhone上运行。
答案 0 :(得分:302)
我刚刚找到了答案,在控制器中使用了这个:
[self.navigationItem setHidesBackButton:YES animated:YES];
并恢复它:
[self.navigationItem setHidesBackButton:NO animated:YES];
-
<强> [UPDATE] 强>
Swift 3.0:
self.navigationItem.setHidesBackButton(true, animated:true)
答案 1 :(得分:27)
添加此代码
[self.navigationItem setHidesBackButton:YES];
答案 2 :(得分:22)
除了删除后退按钮(使用已推荐的方法)之外,请不要忘记用户仍然可以在iOS 7及更高版本中使用从左到右的轻扫手势“弹出”到上一个屏幕。
要禁用它(在适当的时候),请执行以下操作(例如,在viewDidLoad中):
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
答案 3 :(得分:16)
为了有条理地隐藏和显示“后退”按钮,您可以使用以下代码:
-(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中 例如:当您将下一个类的数组更新为上一个类,然后将条件检查到上一个类时,如上所述。
答案 4 :(得分:16)
只是为了澄清现有的答案:hidesBackButton
属性是正确的答案,但在许多答案中并不清楚self
指的是什么。基本上你应该在视图控制器中设置self.navigationItem.hidesBackButton = YES
即将被推(或刚推到)UINavigationController
。
换句话说,我说UINavigationController
名为myNavController
。我想在其上添加一个新视图,当我这样做时,我不希望后退按钮再显示。我可以做类似的事情:
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
控制的视图,并且不应显示后退按钮。
答案 5 :(得分:11)
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
答案 6 :(得分:10)
sethidesbackbutton由于某种原因不适合我
我用这种方式 - &gt;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 30)]] ;
答案 7 :(得分:7)
始终将Apple文档用于简单的问题,它们更简单,更轻便:)
以下是Swift 3.0的语法:
self.navigationItem.setHidesBackButton(true, animated:true)
参考
答案 8 :(得分:6)
就我而言,我目前的答案几乎没有问题:
所以最终对我有用的解决方案是:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self.navigationItem setHidesBackButton:YES animated:NO];
}
return self;
}
答案 9 :(得分:3)
Zoran Simic建议的解决方案由于某种原因不适合我。
此代码确实有效:
MyController* controller = [[MyController alloc] init];
NSArray* array = [[[NSArray alloc] initWithObjects:controller, nil] autorelease];
[self.navigationController setViewControllers:array animated:NO];
[controller release];
显然,您必须根据自己的喜好操纵NSArray,以使其适合您。 希望能帮助某人:)
答案 10 :(得分:1)
在我的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];
}
答案 11 :(得分:0)
这会隐藏后退按钮并将其替换为Swift中的添加按钮:
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
}
}
}
答案 12 :(得分:0)
通常,您应该在本页面上多次使用Apple的per-ViewController API,但有时您需要立即控制“后退”按钮。
以下代码隐藏了“后退”按钮,并确保隐藏按钮区域中不会发生点击碰撞检测。
let emptyView = UIView(frame: .zero)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: emptyView)
答案 13 :(得分:0)
这隐藏了后退按钮
let backBtn = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backBtn