我正在测试一个应用程序,我发现当双击后退按钮时,它会自动弹出到导航 Controller 的 Root View Controller 。没有任何代码明确地执行此操作,我想知道这是否是导航 Controller 中后退按钮的默认行为。
我在另一个应用程序中测试了同样的东西,但问题不存在,所以唯一可以解释这一点的是其他东西触发了 popToRoot 而我无法弄清楚是什么。
抱歉,我不能在这里发布代码。只是想听听您对此的看法。
提前致谢!
最佳答案
正如我上面在 Glenns 回答中评论的那样,将 popViewController
分派(dispatch)到主线程中可能是一个问题,因为它也会在识别出双击时执行。也许这是一个稍微“稳定”的解决方案:
private func setupBackButton() {
let myBackButton = UIButton(type: .custom) // or whatever
// setup title, image etc.
let myCustomBackButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
// Create gesture recognizers for single and double tap:
let singleRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.singleTap))
singleRecognizer.numberOfTapsRequired = 1
let doubleRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.doubleTap))
doubleRecognizer.numberOfTapsRequired = 2
// single tap should only fire if double tap is not recognized
singleRecognizer.require(toFail: doubleRecognizer)
myBackButton.addGestureRecognizer(singleRecognizer)
myBackButton.addGestureRecognizer(doubleRecognizer)
}
@objc private func singleTap() {
self.navigationController?.popViewController(animated: true)
}
@objc private func doubleTap() {
self.navigationController?.popToRootViewController(animated: true)
}
关于ios - 双击后退按钮 UINavigationBar 时弹出到 root,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54249280/