【发布时间】:2017-07-19 17:54:46
【问题描述】:
我想在 react native 应用程序中显示原生视图控制器 (ABPersonViewController)。
我可以呈现视图,但不能推送视图。此代码片段有效并显示视图,但我的应用程序没有后退按钮:
let personViewController = ABPersonViewController()
let rootViewController:UIViewController?
= UIApplication.shared.delegate?.window??.rootViewController!
personViewController.displayedPerson = record!
// this works
rootViewController!.present(personViewController, animated: true)
如果我尝试推送视图而不是呈现视图,则什么也不会发生,因为 navigationController 为 nil:
if (rootViewController!.navigationController != nil) {
rootViewController!.navigationController!.pushViewController(personViewController, animated: true)
}
如何在 react native iOS 中推送原生视图?
在 React native android 中,可以在自定义模块中使用以下代码轻松查看联系人:
final Activity activity = getCurrentActivity();
Intent contactIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id);
contactIntent.setData(uri);
activity.startActivity(contactIntent);
我希望 React Native iOS 能这么简单!
我尝试了@Artal 的 iOS 解决方案,它在一定程度上有效。
我分配了一个 UINavigationController,它使用现有的 RN UIViewController 作为它的根,并将它设置为窗口的 rootViewController。代码如下:
UIViewController *rootViewController = [UIViewController new];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];
navigationController.navigationBarHidden = YES;
rootViewController.view = rootView;
self.window.rootViewController = navigationController;
在我的快速代码中,我将 rootViewController 转换为 UINavigationController,将 isNavigationBarHidden 设置为 false 并推送 personViewController
let navigationController:UINavigationController = rootViewController as! UINavigationController;
DispatchQueue.main.async {
navigationController.isNavigationBarHidden = false;
navigationController.pushViewController(personViewController, animated: true)
}
当我覆盖 UINavigationController ShouldPopOnBackButton 以便在返回 RN 时将 navigationBarHidden 设置为 true 时,问题出现了。我遵循https://stackoverflow.com/a/19132881/826435中的方法
@implementation UINavigationController (ShouldPopOnBackButton)
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if([self.viewControllers count] < [navigationBar.items count]) {
return YES;
}
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationBarHidden = YES;
[self popViewControllerAnimated:YES];
});
return NO;
}
@结束
在调用之后,RN 视图恢复,但随后在模拟器上运行时,所有输入字段的 RN 键盘都被禁用;不过它在设备上运行良好。
知道为什么模拟器上的 RN 键盘被禁用了吗?
詹姆斯
【问题讨论】:
-
据我所知,您所做的似乎无法直接通过 Swift-React 桥接代码完成。参考这个,也许你会找到解决办法。 github.com/facebook/react-native/issues/1148 。此外,请熟悉 iOS 中的自定义过渡,这通常是获得您可能想要的独特过渡的最简单方法。 raywenderlich.com/146692/…
-
出于好奇,您的第一个代码块是在 AppDelegate 中吗?..
-
没有代码在一个单独的 swift 模块中,该模块通过 react native bridge 调用。感谢其他参考;我去看看。
标签: swift react-native