How to disable back button in navigation bar

前端 未结 5 460
一个人的身影
一个人的身影 2020-12-29 01:22

Is there any official way how to set UIBarButtonItem.enabled property? I tried to set a backButtonItem in previous controller. But enabled property

5条回答
  •  灰色年华
    2020-12-29 02:08

    I know this is an old thread, but this may help someone else. As mentioned by hennes, you can no longer disable the back button. Instead, you will need to disable the entire navigationBar.

    The approach I took, was disabling the navigationBar, and then applying an 0.5 alpha to the subviews of the navigation bar.

    In your ViewController class:

        func changeBarButtons(alpha: CGFloat) {
            navigationController?.navigationBar.subviews.forEach { firstViews in
                firstViews.subviews.forEach { view in
                    if ["_UIButtonBarButton", "_UIButtonBarStackView"].contains(type(of: view).description()) {
                        view.alpha = alpha
                    }
                }
            }
        }
    
        func set(loading: Bool) {
            let alpha: CGFloat = loading ? 0.5 : 1
            navigationController?.navigationBar.isUserInteractionEnabled = !loading
    
            changeBarButtons(alpha: alpha)
        }
    

    Keep in mind, that Apple could change the names of the class any time. That being said, it's highly unlikely they do so. If you don't mind the title of the View Controller fading out, you can apply the alpha to all the subviews, without checking the class name.

提交回复
热议问题