简体   繁体   English

iOS如何删除后退按钮?

[英]iOS how to remove back button?

I have an application with a navigation bar that pushes to a login screen view controller and then pushes to a main menu.我有一个带有导航栏的应用程序,它推送到登录屏幕视图控制器,然后推送到主菜单。 Is there any way I can remove the back button off the main menu, so the user is unable to go back to the login screen?有什么办法可以从主菜单中删除后退按钮,使用户无法返回登录屏幕?

Thanks!谢谢!

EDIT: Using Xcode 4.3 and doing all the leg work programmatically.编辑:使用 Xcode 4.3 并以编程方式完成所有腿部工作。

You can do:你可以做:

[self.navigationItem setHidesBackButton:YES];

In your second view controller (the one you want to hide the button in).在你的第二个视图控制器(你想隐藏按钮的那个)中。

Peters answer is correct, although I think the better question is why?彼得斯的回答是正确的,尽管我认为更好的问题是为什么? In a schema like yours where you are wanting to login a user, instead of using a Pushed VC, present a Modal VC and use a delegate method to get back the userinfo that was obtained in the Login process.在像您这样的架构中,您想要登录用户,而不是使用 Pushed VC,而是提供 Modal VC 并使用委托方法取回在登录过程中获得的用户信息。 I can post a complete code example if you need it, but it sounds like you have the details worked out with your login process.如果您需要,我可以发布完整的代码示例,但听起来您已经在登录过程中制定了详细信息。 Just use:只需使用:

presentModalViewController

instead of:代替:

pushViewController

That way, you don't have to worry about the navigation stack and doing something that isn't really in-line with the user interface guidelines.这样,您就不必担心导航堆栈和做一些与用户界面指南不符的事情。

在迅速

self.navigationItem.hidesBackButton = true

The above code did not work for me.上面的代码对我不起作用。 As suggested in UINavigationItem setHidesBackButton:YES won't prevent from going back , I had to use:正如UINavigationItem setHidesBackButton:YES 中所建议的那样不会阻止返回,我不得不使用:

[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] init]]];

Try this:尝试这个:

[self.navigationItem setHidesBackButton:YES];

Or或者

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

Tried in Xcode7.3.1, swift在Xcode7.3.1中试过,迅捷

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

It only hide the back arrow and disabled the back action, but I can still see the name of the previous view controller.它只隐藏后退箭头并禁用后退动作,但我仍然可以看到前一个视图控制器的名称。

For those who want to also hide the name of the previous view controller, try Yoga's answer works for me.对于那些还想隐藏前一个视图控制器名称的人,试试Yoga 的答案对我有用。 In swift在迅速

self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIView())

In the case you need to toggle show/hide the back button:如果您需要切换显示/隐藏后退按钮:

navigationItem.hidesBackButton = true/false

And keep the swipe back gesture:并保持向后滑动手势:

extension YourViewController: UIGestureRecognizerDelegate {}

And

navigationController?.interactivePopGestureRecognizer?.isEnabled = true
navigationController?.interactivePopGestureRecognizer?.delegate = self

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

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