简体   繁体   English

在 UINavigationController 中覆盖向后滑动手势

[英]Overriding back swipe gesture in UINavigationController

How to override gesture to pop to rootViewController, not to previous ViewController?如何覆盖手势以弹出到 rootViewController,而不是以前的 ViewController?

You can do this in a combination of the following:您可以结合以下方式执行此操作:

Add a swipe gesture recognizer to your view controller:向您的视图控制器添加滑动手势识别器:

在此处输入图片说明

Add the following to your view controller class:将以下内容添加到您的视图控制器类:

import UIKit

class SwipeBackViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }

    @IBAction func swipeback(_ sender: UISwipeGestureRecognizer) {
        navigationController?.popToRootViewController(animated: true)
    }
}
  • The command in viewDidLoad disables the default swipe recogninzer in iOS viewDidLoad的命令禁用了 iOS 中的默认滑动识别器
  • Then, the action associated with the swipe recognizer you added above handles the pop for you然后,与您在上面添加的滑动识别器相关联的操作会为您处理弹出窗口

My answer here goes into disabling the recognizer in more detail in case you have any questions on that.我的答案在这里进入的情况下,禁用识别更详细你有任何问题。

I followed CodeBender's answer but the swipeback function didn't call.我遵循了 CodeBender 的回答,但没有调用swipeback功能。

After add self.view.addGestureRecognizer(backswipeGestureRecognizer) in viewDidLoad() , it works.viewDidLoad()添加self.view.addGestureRecognizer(backswipeGestureRecognizer)后,它起作用了。

Also, you can set delegate of this gesture in viewController, in viewController's viewDidLoad.此外,您可以在 viewController 的 viewDidLoad 中设置此手势的委托。 It looks like:看起来像:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController.interactivePopGestureRecognizer?.delegate = self
}

and customize it's methods in view controller. For example:并在视图 controller 中自定义它的方法。例如:

   func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive event: UIEvent) -> Bool {
    print("event \(event.description)")
    return true

} }

If you set returning value for false - pop action will not be triggered如果您将返回值设置为 false - 将不会触发弹出操作

For me it worked like:对我来说,它的工作方式如下:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive event: UIEvent) -> Bool {
    showAlertViewController()
    return false

} }

If you need this behaviour only once, you should set delegate back in viewController's deinit method如果你只需要一次这种行为,你应该在 viewController 的 deinit 方法中重新设置委托

deinit {
    navigationController?.interactivePopGestureRecognizer?.delegate = navigationController
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 iOS 上的 UINavigationController 中禁用向后滑动手势 7 - How to disable back swipe gesture in UINavigationController on iOS 7 将UIPageViewController滑动与iOS 7 UINavigationController反滑动手势相结合 - Combine UIPageViewController swipes with iOS 7 UINavigationController back-swipe gesture 增加在UINavigationController上调用向后滑动手势所需的插入 - to increase the inset required to invoke the swipe back gesture on UINavigationController 如何在iOS 7的UINavigationController中创建自定义向后滑动手势 - How to create custom back swipe gesture in UINavigationController on iOS 7 如何在iOS 8中的UINavigationController中禁用后退滑动手势? - How do disable back swipe gesture in UINavigationController in iOS 8? 设置 leftBarButtonItem 后如何在 UINavigationController 中启用向后/向左滑动手势? - How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem? UINavigationController向下滑动手势检测 - UINavigationController swipe down gesture detect 获取UINavigationController向后滑动的进度 - Get progress of UINavigationController swipe back 在UINavigationController中控制向后滑动操作 - Control swipe back action in UINavigationController UINavigationController滑动即可返回问题 - UINavigationController swipe to go back issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM