【问题标题】:Prevent Lockscreen / Notificationcenter from swiping down in react-native防止锁定屏幕/通知中心在 react-native 中向下滑动
【发布时间】:2018-08-20 08:51:24
【问题描述】:

从 iOS 11 开始,将自己的手势优先于屏幕边缘上的系统手势的行为发生了变化。

以前 iOS 假设如果您隐藏状态栏,您希望屏幕边缘上的手势首先被触发。

现在您必须重写 preferredScreenEdgesDeferringSystemGestures 方法才能获得与此处所述相同的结果:https://useyourloaf.com/blog/avoiding-conflicts-with-system-gestures-at-screen-edges/

我们如何在 react-native 中做到这一点?这是否已经在最近的版本中处理?我在 react-native 源代码中找不到对该方法的任何引用。

【问题讨论】:

    标签: javascript ios react-native


    【解决方案1】:

    这可以通过将正在使用的 UIViewController 交换为以您喜欢的方式实现 preferredScreenEdgesDeferringSystemGestures 的方式来完成。

    首先创建一个新类,例如MainViewController,返回所需的值:

    //
    //  MainViewController.h
    //
    
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface MainViewController : UIViewController
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    和:

    //
    //  MainViewController.m
    //
    
    #import "MainViewController.h"
    
    @interface MainViewController ()
    
    @end
    
    @implementation MainViewController
    
    - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures
    {
      return UIRectEdgeBottom;
    }
    
    @end
    

    最后,将通用视图控制器换成我们刚刚创建的专用视图控制器:

      */
    
     #import "AppDelegate.h"
    +#import "MainViewController.h"
    
     #import <React/RCTBundleURLProvider.h>
     #import <React/RCTRootView.h>
    
    ===== SNIP =====
    
       rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    
       self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    -  UIViewController *rootViewController = [UIViewController new];
    +  UIViewController *rootViewController = [MainViewController new];
       rootViewController.view = rootView;
       self.window.rootViewController = rootViewController;
       [self.window makeKeyAndVisible];
    

    【讨论】:

      【解决方案2】:

      我设法在 UIViewController 上使用 Swift extension 调配方法解决了这个问题:

      extension UIViewController {
      fileprivate static func swizzleReduceSystemGestures() {
            guard let aClass = NSClassFromString("RNNNavigationController") ?? NSClassFromString("RNNStackController"), // aClass?.alloc() as? UINavigationController
            let originalMethod = class_getInstanceMethod(aClass, #selector(getter: UIViewController.preferredScreenEdgesDeferringSystemGestures)),
            let swizzledMethod = class_getInstanceMethod(aClass, #selector(getter: preferredScreenEdgesDeferringSystemGesturesSwizzled)) else { return }
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
      
        @objc public var preferredScreenEdgesDeferringSystemGesturesSwizzled: UIRectEdge {
             return .all
        }
      }
      

      然后在你的AppDelegate中调用这个静态方法:

       UIViewController.swizzleReduceSystemGestures()
      

      同样,您可以使用 swizzle 设置 prefersHomeIndicatorAutoHidden,它就像一个魅力,直到他们给我们一个合适的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-01
        • 1970-01-01
        相关资源
        最近更新 更多