【问题标题】:Prevent Lockscreen / Notificationcenter from swiping down in react-native防止锁定屏幕/通知中心在 react-native 中向下滑动
【发布时间】:2018-08-20 08:51:24
【问题描述】:
【问题讨论】:
标签:
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,它就像一个魅力,直到他们给我们一个合适的解决方案。