【问题标题】:React-Native iOS - How to show a view controller after Launch screen, before initialising react native bridge (iOS)React-Native iOS - 如何在启动屏幕之后显示视图控制器,然后再初始化反应原生桥(iOS)
【发布时间】:2020-06-03 09:06:16
【问题描述】:

我有一个位于原生 iOS 视图控制器 (swift) 中的动画(启动屏幕动画),我想在启动本机桥接之前在启动屏幕后 3 秒显示该动画的视图 vontroller。

我尝试使用 appdelegate.m 文件,这就是我想出的结果????

RN 0.61 版

#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"AnimationView" bundle:[NSBundle mainBundle]];
     UIViewController *vc =[storyboard instantiateInitialViewController];


  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"SplashScreenFinal"
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     self.window.rootViewController = vc;
     [self.window makeKeyAndVisible];


  if (after 3 secs) {
      self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
      UIViewController *rootViewController = [UIViewController new];
      rootViewController.view = rootView;
      self.window.rootViewController = rootViewController;
      [self.window makeKeyAndVisible];

  }
  return YES;
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end

谢谢。

【问题讨论】:

    标签: ios objective-c swift react-native splash-screen


    【解决方案1】:
    • 您调用方法[storyboard instantiateInitialViewController] 此方法将调用您为此情节提要设置默认值的create ViewController。您是否检查了Is Initial View Controller 以获得要显示的第一个 ViewController?

    • 您可以使用另一种方式来初始化视图控制器[storyboard instantiateViewControllerWithIdentifier:@"myAnimationViewController"]; 记住为要设置 rootViewController 的视图控制器设置 Storyboard ID myAnimationViewController

    代码示例:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"AnimationViewStoryboard" bundle:[NSBundle mainBundle]];
      // this code just run when storyboard have default ViewController
    //  UIViewController *vc =[storyboard instantiateInitialViewController];
    
      UIViewController *vc =[storyboard         instantiateViewControllerWithIdentifier:@"myAnimationViewController"];
    
      RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
      RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"DemoReactNative"
                                            initialProperties:nil];
    
      rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
    
      UIViewController *rootViewController = [UIViewController new];
      rootViewController.view = rootView;
    
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      self.window.rootViewController = vc;
      [self.window makeKeyAndVisible];
    
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.window.rootViewController = rootViewController;
      });
    
      return YES;
    }
    

    【讨论】:

    • 嗨@JackDao,感谢您的回答(欢迎来到 StackOverflow ?)。我现在就试试这个。
    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2016-03-05
    相关资源
    最近更新 更多