How to disable back button in navigation bar

前端 未结 5 461
一个人的身影
一个人的身影 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 01:59

    Don't try to disable your custom back button (won't work), just set a new one which is disabled. You can reach the previous navigation item through the UINavigationBar.backItem property.

    // set disabled back button
    let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
    backButton.isEnabled = false
    navigationController?.navigationBar.backItem?.backBarButtonItem = backButton
    
    // disable pop gesture
    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    

提交回复
热议问题