繁体   English   中英

UINavigationController 交互式流行手势不起作用?

[英]UINavigationController Interactive Pop Gesture Not Working?

所以我在我为 iOS 7 应用程序构建的中有一个导航控制器。 titleView 是可见的,以及后退按钮和导航栏本身。 出于某种原因,交互式弹出手势(从左边缘滑动)不起作用。 什么都没发生。 当我记录手势时,它不是零。 要启用此功能,我需要做什么特别的事情吗? 什么可能导致它不起作用?

我发现当使用自定义后退按钮时,交互式弹出手势停止工作(我的看法是 Apple 无法预见您的自定义后退按钮的行为,因此他们禁用了该手势)。

为了解决这个问题,正如前面提到的,你可以将interactivePopGestureRecognizer.delegate属性设置为nil

在斯威夫特, 通过像这样为UINavigationController添加扩展,这可以在整个应用程序中轻松完成

extension UINavigationController {

    override public func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = nil
    }

}

更新答案

似乎将委托设置为nil会导致应用程序 UI 在某些情况下冻结(例如,当用户在导航堆栈的顶部视图控制器上向左或向右滑动时)。

因为无法在扩展中处理gestureRecognizerShouldBegin委托方法,所以子类化UINavigationController似乎是最好的解决方案:

class NavigationController: UINavigationController, UIGestureRecognizerDelegate {

    /// Custom back buttons disable the interactive pop animation
    /// To enable it back we set the recognizer to `self`
    override func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }

    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }

}

呃,看起来我只需要设置手势委托并实现以下内容:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;

}

看看这个回复和评论。 您所要做的就是将导航控制器的交互式流行手势识别器的委托设置为nil

self.navigationController.interactivePopGestureRecognizer.delegate = nil;

将其设置为id<UIGestureRecognizerDelegate>的铸造 self 也有效,因为协议中的所有方法都是可选的,但我认为在这种情况下将委托设置为nil更合适。

您可以将此行放在viewDidLoad方法中。

self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;

我的答案基于 Eneko 的答案,但仅使用 UINavigationController 上的扩展并在 Swift 5 中工作:

extension UINavigationController: UIGestureRecognizerDelegate {

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

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }
}

如果你觉得你已经尝试了所有的解决方案并伸展你的头脑,那么你来对地方了。

Goto simulator > Window > Enable Show Device Bezels

在此处输入图片说明

现在尝试模拟向后滑动手势。

更有效的答案是 Aaron 和 lojals

先自定义导航控制器,然后把这段代码放到类中

在 ViewDidload 中放置这一行:

self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;

在课堂上写这个函数

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES;}

也许有人会觉得这很有帮助。

如果你想隐藏导航栏但使用普通的滑动手势返回和其他导航控制器功能,你应该使用:( navigationBar

self.navigationController?.navigationBar.isHidden = true

如果您想禁用导航栏(隐藏导航栏,禁用向后滑动)但想推动视图控制器,您应该使用:( isNavigationBarHidden

self.navigationController?.isNavigationBarHidden = true

2018 年 12 月 7 日更新:

推荐方式:

如果您的第一个控制器使用隐藏导航栏,但下一个孩子使用导航栏,当您返回基本视图控制器时,您将看到一个黑色栏代替导航栏。 如果您在第一个视图控制器(父亲)中使用,这将很容易解决:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

在 Swift 4 中,我的视图控制器中有一个 UITableView,我解决了这个问题:

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

一般为整个应用程序添加交互式弹出手势。

XCODE :9.0,斯威夫特:4.0

最好在 AppDelegate.swift 中创建 UINavigationController

  1. 创建导航控制器
// I created a global variable, however not necessarily you will be doing this way
var nvc: UINavigationController!
  1. 实现UIGestureRecognizerDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, UIGestureRecognizerDelegate {
  1. 在应用程序中实例化UINavigationController didFinishLaunchingWithOptions 函数
nvc=UINavigationController()

// For interactive pop gesture
nvc.navigationBar.isHidden=true
nvc?.interactivePopGestureRecognizer?.delegate=self
  1. 额外的步骤,在应用 didFinishLaunchingWithOptions 函数中将控制器添加到导航控制器
window=UIWindow()
window?.rootViewController=nvc
window?.makeKeyAndVisible()

// BaseViewController is sample controller i created with xib
nvc.pushViewController(BaseViewController(), animated: true)
  1. 实现姿态识别器,将以下代码添加到 AppDelegate.swift
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }



注意:请参阅本节中的其他帖子以了解两者之间的区别

self.navigationController?.navigationBar.isHidden=true

self.navigationController?.isNavigationBarHidden = true

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM