我想在react本机应用程序中显示本机视图控制器(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)
}
如何在本机iOS内推送原生视图?
在React本机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);
我希望在原生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;
在我的swift代码中,我将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;
}
@结束
调用AftershouldPopItem,恢复RN视图,但随后在模拟器上运行时,所有输入字段都会禁用RN键盘;它在设备上工作正常 .
有关为什么模拟器上的RN键盘被禁用的想法?
詹姆士
2 回答
您无法推送视图控制器,因为RN应用程序的默认
rootViewController
是UIViewController
而不是UINavigationController
,并且's also why you see that it' snil
.如果您想使用本机导航堆栈并推送另一个本机屏幕,您有两个选择:
使用本机导航解决方案,例如RNN . 这种方法的缺点是它需要改变你当前处理导航的方式 . Note :虽然RN附带了一个开箱即用的原生导航解决方案(NavigatorIOS),但它赢得了't allow you to push other native screens. You might be able to hack it by accessing some private properties but i' m,不确定你是否想去那里 .
在
AppDelegate
中更改应用的根目录 . 您可以分配UINavigationController
,它将使用现有的RNUIViewController
作为其根,并将其设置为窗口的rootViewController
. 请注意,此类更改可能会对您的应用产生其他影响;例如:您将获得UINavigationController
原生导航栏以及可能的其他一些副作用 .@Artal建议的解决方案(2)可以在设备上运行,但在模拟器上,键盘在从本机视图返回后被禁用 .