从iOS 11开始,在屏幕边缘将自己的手势优先于系统手势的行为发生了变化。
以前,iOS假设如果你隐藏状态栏,你希望屏幕边缘的手势首先被触发。
现在,您必须重写preferredScreenEdgesDeferringSystemGestures方法以获得与此处解释相同的结果:https://useyourloaf.com/blog/avoiding-conflicts-with-system-gestures-at-screen-edges/。
我们如何在react-native中做到这一点?在最近的版本中是否已经处理了这个问题?我在react-native源代码中找不到任何对该方法的引用。
发布于 2018-12-30 05:06:57
这可以通过将正在使用的UIViewController交换为以您喜欢的方式实现preferredScreenEdgesDeferringSystemGestures的you来实现。
首先创建一个返回所需值的新类,例如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];发布于 2019-11-11 17:58:29
我设法通过在UIViewController上使用Swift extension的方法swizzle解决了这个问题
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,它就像一个护身符,直到他们给我们一个适当的解决方案。
https://stackoverflow.com/questions/51927188
复制相似问题