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

时间:2023-01-21 14:42:40

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.

有没有官方的方法如何设置UIBarButtonItem.enabled属性?我试图在以前的控制器中设置backButtonItem。但是启用的属性被忽略。

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.

编辑:我不想隐藏它,只是用暗灰色禁用它并禁用用户交互。这与禁用的leftBarButtonItem完全相同。

3 个解决方案

#1


53  

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.

截至今天,无法使用enabled属性禁用后退按钮。除非您创建自定义项,否则backBarButtonItem属性将为nil,即使这样,它也将忽略enabled属性。这有几种(非令人满意的)方法。

Hide the button

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.

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

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

遗憾的是,这会影响导航栏中的其他元素,因此,如果您在右侧有另一个条形按钮项,则可能不会选择它。

Use a Custom Left Bar Button Item

The leftBarButtonItem does not ignore the enabled property 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?.enabled = false

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

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

但是,这将没有带有前三角形指示器的后挡条按钮样式。

#2


4  

If you want to hide it, UInavigationItem has a hidesBackButton property.

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

#3


4  

Add below code in your ViewController2.swift Class.

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

override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.hidesBackButton = true;
    }

It will hide your back button.

它会隐藏你的后退按钮。

#1


53  

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.

截至今天,无法使用enabled属性禁用后退按钮。除非您创建自定义项,否则backBarButtonItem属性将为nil,即使这样,它也将忽略enabled属性。这有几种(非令人满意的)方法。

Hide the button

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.

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

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

遗憾的是,这会影响导航栏中的其他元素,因此,如果您在右侧有另一个条形按钮项,则可能不会选择它。

Use a Custom Left Bar Button Item

The leftBarButtonItem does not ignore the enabled property 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?.enabled = false

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

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

但是,这将没有带有前三角形指示器的后挡条按钮样式。

#2


4  

If you want to hide it, UInavigationItem has a hidesBackButton property.

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

#3


4  

Add below code in your ViewController2.swift Class.

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

override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.hidesBackButton = true;
    }

It will hide your back button.

它会隐藏你的后退按钮。