ios - 双击后退按钮 UINavigationBar 时弹出到 root

标签 ios swift xcode

我正在测试一个应用程序,我发现当双击后退按钮时,它会自动弹出到导航 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/

相关文章:

javascript - 保存 UIWebView 中的高亮文本

ios - UIGraphicsBeginImageContext 不返回视网膜图像

ios - 结合 convert Just 到 AnyPublisher

ios - 使用 Tabman 分页 View Controller ,可以将默认的 Tab 键顺序更改为 RTL 吗?

ios - 无法将分发配置文件添加到 Xcode 5

ios - 在标签中滑动文本

ios - 需要在一次购买中结合消耗品和非续订订阅

swift - 带 header 的 Alamofire POST 请求

swift - Xcode无缘无故创建断点

iOS 应用只能发送 125 条 UDP 消息?