ios 如何禁用导航栏中的后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32010429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:16:10  来源:igfitidea点击:

How to disable back button in navigation bar

iosswiftstoryboard

提问by Deny

Is there any official way how to set UIBarButtonItem.enabledproperty? I tried to set a backButtonItem in previous controller. But enabledproperty is ignored.

有没有官方的方法来设置UIBarButtonItem.enabled属性?我试图在以前的控制器中设置一个 backButtonItem。但是enabled财产被忽略了。

More in this simple example project.

更多在这个简单的示例项目中

I don't want to some solution like "make your own leftBarButtonItem and set its alpha ..."

我不想像“制作自己的 leftBarButtonItem 并设置它的 alpha 之类的解决方案......”

Edit: 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.

编辑:我不想隐藏它,只能用变暗的颜色和禁用的用户交互来禁用它。这与 disabled 的行为完全相同leftBarButtonItem

回答by hennes

As of today it is not possible to disable the back button using the enabledproperty. The backBarButtonItemproperty will be nilunless you create a custom item and even then it will ignore the enabledproperty. There are a couple (non-satisfactory) ways around this.

截至今天,无法使用该enabled属性禁用后退按钮。除非您创建自定义项目,否则该backBarButtonItem属性将是nil,即使这样它也会忽略该enabled属性。有几种(不令人满意的)方法可以解决这个问题。

Hide the button

隐藏按钮

This is what Apple wants you to do given that they ignore the enabledproperty. It is as simple as

鉴于他们忽略了该enabled属性,这就是 Apple 希望您执行的操作。就这么简单

navigationItem.hidesBackButton = true  

and should be the preferred approach unless you have good reasons.

除非您有充分的理由,否则应该是首选方法。

Disable and Tint the Navigation Bar

禁用和着色导航栏

You can disable user interaction on the whole navigation bar and tint it to make the back button appear disabled.

您可以在整个导航栏上禁用用户交互并对其进行着色以使后退按钮显示为禁用。

navigationController?.navigationBar.isUserInteractionEnabled = false
navigationController?.navigationBar.tintColor = UIColor.lightGray

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.

不幸的是,这也会影响导航栏中的其他元素,因此,例如,如果右侧有另一个栏按钮项,则它可能不是一个选项。

Use a Custom Left Bar Button Item

使用自定义左栏按钮项

The leftBarButtonItemdoes not ignore the enabledproperty so you could create a custom item and trigger the pop manually when it is activated.

leftBarButtonItem不忽略的enabled属性,以便您可以创建一个自定义项目和手动触发弹出时被激活。

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ThisClass.backButtonTapped))
...
navigationItem.leftBarButtonItem?.isEnabled = false

func backButtonTapped() {
    self.navigationController?.popViewController(animated: true)
}

This will, however, not have the back bar button style with the leading triangular indicator.

但是,这不会具有带前导三角形指示器的后栏按钮样式。

回答by BKjadav

Add below code in your ViewController2.swift Class.

在您的 ViewController2.swift 类中添加以下代码。

override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.hidesBackButton = true;
    }

It will hide your back button.

它会隐藏你的后退按钮。

回答by Templar

If you want to hide it, UInavigationItemhas a hidesBackButtonproperty.

如果你想隐藏它,UInavigationItem有一个hidesBackButton属性。