您知道如何在UINavigationController中隐藏“后退”按钮吗?另外,如何显示回来,但是我想这与隐藏它非常相似...
就像在查看电子邮件时单击“编辑”时,邮件应用程序在iPhone上所做的一样。
您知道如何在UINavigationController中隐藏“后退”按钮吗?另外,如何显示回来,但是我想这与隐藏它非常相似...
就像在查看电子邮件时单击“编辑”时,邮件应用程序在iPhone上所做的一样。
Answers:
我刚刚找到答案,在控制器中使用以下命令:
[self.navigationItem setHidesBackButton:YES animated:YES];
并还原它:
[self.navigationItem setHidesBackButton:NO animated:YES];
-
[更新]
Swift 3.0:
self.navigationItem.setHidesBackButton(true, animated:true)
viewWillAppear:animated
隐藏并viewWillDisappear:animated
还原它。
除了删除后退按钮(使用已经推荐的方法)之外,不要忘记用户仍然可以在iOS 7及更高版本中使用从左到右的滑动手势来“弹出”上一个屏幕。
要禁用它(在适当的情况下),请实现以下功能(例如,在viewDidLoad中):
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
只是为了澄清现有答案:该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
现在应显示由控制的视图,并且不应显示任何后退按钮。
为了有条件地隐藏和显示“后退”按钮,可以使用以下代码:
-(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中,例如:当将下一类的数组更新为上一类,然后如上所述检查条件进入下一类时。
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
始终使用Apple文档解决简单的问题,因为它们更直接,更轻便:)
这是Swift 3.0的语法:
self.navigationItem.setHidesBackButton(true, animated:true)
参考
就我而言,我对当前答案的看法很少:
因此,最终对我有用的解决方案是:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self.navigationItem setHidesBackButton:YES animated:NO];
}
return self;
}
Zoran Simic提出的解决方案由于某种原因对我不起作用。
这段代码确实起作用了:
MyController* controller = [[MyController alloc] init];
NSArray* array = [[[NSArray alloc] initWithObjects:controller, nil] autorelease];
[self.navigationController setViewControllers:array animated:NO];
[controller release];
显然,您必须根据自己的喜好操纵NSArray才能使其正常工作。希望能帮助到别人:)
这会隐藏后退按钮,并在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
}
}
}
setHidesBackButton:
在将navigationItem推入navigationController之前,必须先调用。