gpt4 book ai didi

swift - React Native Push 而不是 Present native iOS View

转载 作者:行者123 更新时间:2023-11-28 08:05:43 24 4
gpt4 key购买 nike

我想在 React native 应用程序中显示 native View Controller (ABPersonViewController)。

我可以呈现 View ,但无法推送 View 。此代码片段有效并显示 View ,但我的应用程序没有后退按钮:

let personViewController = ABPersonViewController()
let rootViewController:UIViewController?
= UIApplication.shared.delegate?.window??.rootViewController!

personViewController.displayedPerson = record!

// this works
rootViewController!.present(personViewController, animated: true)

如果我尝试推送 View 而不是呈现 View ,则不会发生任何事情,因为 navigationController 为 nil:

if (rootViewController!.navigationController != nil) {
rootViewController!.navigationController!.pushViewController(personViewController, animated: true)
}

如何在 React Native iOS 中推送原生 View ?

在 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;
}

@结束

在调用 shouldPopItem 之后,RN View 被恢复,但是当在模拟器上运行时,RN 键盘随后对所有输入字段禁用;不过它在设备上运行良好。

关于为什么模拟器上的 RN 键盘被禁用的任何想法?

詹姆斯

最佳答案

您不能推送 View Controller ,因为 RN 应用程序的默认 rootViewControllerUIViewController 而不是 UINavigationController,这就是还有为什么你看到它是 nil

如果您想使用 native 导航堆栈并推送另一个 native 屏幕,您有两个选择:

  1. 使用原生导航解决方案,例如 RNN .这种方法的缺点是它需要完全改变您当前处理导航的方式。 注意:尽管 RN 为原生导航提供了开箱即用的解决方案 ( NavigatorIOS ),但它不允许您推送其他原生屏幕。您也许可以通过访问一些私有(private)属性(property)来破解它,但我不确定您是否想去那里。
  2. AppDelegate 中更改应用程序的根目录。您可以分配一个 UINavigationController,它将使用现有的 RN UIViewController 作为其根并将其设置为窗口的 rootViewController。请注意,这种更改可能会对您的应用产生其他影响;例如:您将获得 UINavigationController native 导航栏和可能的其他一些副作用。

关于swift - React Native Push 而不是 Present native iOS View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45197759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com