响应原生推送而不是呈现原生 iOS 视图 [英] React Native Push rather than Present native iOS view

查看:53
本文介绍了响应原生推送而不是呈现原生 iOS 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在本机应用程序中显示本机视图控制器 (ABPersonViewController).

I want to display a native view controller (ABPersonViewController) within a react native application.

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

I can present the view but I cannot push the view. This code fragment works and presents the view but there is no back button to my app:

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

personViewController.displayedPerson = record!

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

如果我尝试推送视图而不是呈现视图,则没有任何反应,因为 navigationController 为零:

If I try to push the view rather than present the view, nothing happens because navigationController is nil:

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

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

How can I push a native view within react native iOS?

在 React Native android 中,可以在自定义模块中使用此代码轻松查看联系人:

In React native android, view contacts can easily be done using this code in a custom module:

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 做出反应如此简单!

I wish it was that easy in react native iOS!

我尝试了@Artal 的 iOS 解决方案,它在一定程度上有效.

I tried @Artal's solution for iOS and it works up to a point.

我分配了一个 UINavigationController,它使用现有的 RN UIViewController 作为它的根,并将它设置为窗口的 rootViewController.代码如下:

I allocated a UINavigationController that used the existing RN UIViewController as its root and set it as the rootViewController of the window. Here is the code:

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

In my swift code I then cast the rootViewController to UINavigationController, set isNavigationBarHidden to false and pushed the 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

The problem came when I overrode the UINavigationController ShouldPopOnBackButton in order to set navigationBarHidden to true when I returned to RN. I followed the approach in 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;
}

@end

AftershouldPopItem 被调用,RN 视图被恢复,但随后在模拟器上运行时,RN 键盘对于所有输入字段都被禁用;不过它在设备上运行良好.

AftershouldPopItem is called, the RN view is restored but the RN keyboard is then subsequently disabled for all input fields when running on the simulator; it works fine on a device though.

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

Any ideas why the RN keyboard on the simulator is disabled?

詹姆斯

推荐答案

也许你可以将 Objective-C navigationController 传递给 Swift 方法.

(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController 可以在 React Native 中转换为 navigationController.

(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController can convert to navigationController in React Native.

但是

UIApplication.shared.keyWindow?.rootViewController as?UINavigationController

这篇关于响应原生推送而不是呈现原生 iOS 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆