使用backBarButtonItem禁用动画

时间:2017-08-22 06:53:08

标签: ios swift uinavigationcontroller uinavigationitem

正如我的标题所说,有没有办法自定义默认后退按钮操作方法?

我试图在Google上找到但没有得到满足我要求的任何答案。

我知道将UIBarButtonItem添加到self.navigationItem.leftBarButtonItem并自定义操作方法的一种方法,但我会隐藏< 返回按钮的标志,我不想要这个。

由Google完成的一些R& D。

1)How to override self.navigationItem.backBarButtonItem Action?

2)iOS disable animation for NavigationController back button

3)Google Question/Answer

添加看似<Back按钮的图像,这是一种方式,但我希望以原生方式进行。

3 个答案:

答案 0 :(得分:1)

一种选择是在View Controller上实现viewWillDisappear方法并设置animationEnable否

override func viewWillDisappear(animated : Bool) {
super.viewWillDisappear(animated)
   UIView.setAnimationsEnabled(false)
} 
override func viewDidDisappear(_ animated: Bool) {
      UIView.setAnimationsEnabled(false)
}

答案 1 :(得分:0)

你必须在那里添加自定义栏按钮......

override func viewDidLoad() {
        super.viewDidLoad()
         let button = UIButton(frame: YourDesiredFrame)
        button.setImage(UIImage(named : "back"), for: .normal)
        button.addTarget(self, action: #selector(backButtonTapped(_:)), for: .touchUpInside)
        let barButton = UIBarButtonItem(customView: button)
        self.navigationItem.backBarButtonItem = barButton
}


    func backButtonTapped(_ sender : UIButton) {
        _ = self.navigationController?.popViewController(animated : false)
    }

答案 2 :(得分:0)

// Swift 4
override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    // turn on animations
    UIView.setAnimationsEnabled(true)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // turn off animations
    UIView.setAnimationsEnabled(false)
}