如何禁用导航栏中的后退按钮 [英] How to disable back button in navigation bar
问题描述
有没有官方的方法如何设置 UIBarButtonItem.enabled
属性?我试图在以前的控制器中设置backButtonItem。但是已启用
属性将被忽略。
Is there any official way how to set UIBarButtonItem.enabled
property? I tried to set a backButtonItem in previous controller. But enabled
property is ignored.
此简单示例项目。
我不想要一些像制作你自己的leftBarButtonItem并设置它的alpha ...这样的解决方案
I don't want to some solution like "make your own leftBarButtonItem and set its alpha ..."
编辑:我不想隐藏它,只禁用暗色和禁用用户相互作用。这与禁用 leftBarButtonItem
的行为完全相同。
I don't want to hide it, only disable it with dimmed colour and disabled user interaction. It's exactly the same behaviour as for disabled leftBarButtonItem
.
推荐答案
从此开始今天,无法使用启用
属性禁用后退按钮。 backBarButtonItem
属性将是 nil
除非您创建自定义项目,否则它将忽略启用
属性。有几种(不令人满意的)方法。
As of today it is not possible to disable the back button using the enabled
property. The backBarButtonItem
property will be nil
unless you create a custom item and even then it will ignore the enabled
property. There are a couple (non-satisfactory) ways around this.
这就是Apple希望你这样做,因为他们忽略了启用的
属性。这很简单
This is what Apple wants you to do given that they ignore the enabled
property. It is as simple as
navigationItem.hidesBackButton = true
并且应该是首选方法,除非你有充分的理由。
and should be the preferred approach unless you have good reasons.
您可以在整个导航栏上禁用用户互动并对其进行着色以使后退按钮显示为禁用。
You can disable user interaction on the whole navigation bar and tint it to make the back button appear disabled.
navigationController?.navigationBar.userInteractionEnabled = false
navigationController?.navigationBar.tintColor = UIColor.lightGrayColor()
不幸的是,这会影响导航栏中的其他元素,因此,如果您在右侧有另一个条形按钮项,则可能不会选择它。
This does, unfortunately, affect other elements in the navigation bar as well so it might not be an option if, for instance, you have another bar button item on the right side.
leftBarButtonItem
不忽略启用
属性,这样您就可以创建自定义项目并在激活时手动触发弹出窗口。
The leftBarButtonItem
does not ignore the enabled
property so you could create a custom item and trigger the pop manually when it is activated.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ThisClass.backButtonTapped))
...
navigationItem.leftBarButtonItem?.enabled = false
func backButtonTapped() {
self.navigationController?.popViewController(animated: true)
}
然而,这将没有后退栏按钮样式领先的三角指标。
This will, however, not have the back bar button style with the leading triangular indicator.
这篇关于如何禁用导航栏中的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!