ios知识

iOS13启动时黑屏

2020-05-19  本文已影响0人  SmileYang966

iOS13引进了SceneDelegate,据说是为了ipad的多屏幕适配。我在像往常一样创建self.window,并指定其tabbarController为window的rootViewController,发现屏幕黑屏。网上搜了一圈,发现自从引入了sceneDelegate,window的创建方式需要作出一些改变。

AppDelegate.m文件

@property(nonatomic,strong)UIWindow *window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    if (@available(iOS 13.0,*)) {
        return YES;
    }else{
        self.window = [[UIWindow alloc]initWithFrame:UIScreen.mainScreen.bounds];
        
        UIViewController *vc = [[UIViewController alloc]init];
        vc.view.backgroundColor = UIColor.blueColor;
        self.window.rootViewController = vc;
        [self.window makeKeyWindow];
        return YES;
    }
}


#pragma mark - UISceneSession lifecycle


- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
 
    if (@available(iOS 13.0,*)) {
           return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
    }else{
        return nil;
    }
}


- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

SceneDelegate.m中

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
    //iOS13以上的版本
    if (@available(iOS 13.0,*)) {
        //UIWindowScene类型的对象
        UIWindowScene *windowScene = (UIWindowScene *)scene;
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [self.window setWindowScene:windowScene];
        [self.window setBackgroundColor:[UIColor whiteColor]];
        
        UIViewController *vc1 = [[UIViewController alloc]init];
        vc1.view.backgroundColor = UIColor.redColor;
        UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
        
        UIViewController *vc2 = [[UIViewController alloc]init];
        vc2.view.backgroundColor = UIColor.orangeColor;
        UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
        
        UIViewController *vc3 = [[UIViewController alloc]init];
        vc3.view.backgroundColor = UIColor.blueColor;
        UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:vc3];
        
        UIViewController *vc4 = [[UIViewController alloc]init];
        vc4.view.backgroundColor = UIColor.brownColor;
        UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];

        UITabBarController *tabBarController = [[UITabBarController alloc]init];
        tabBarController.viewControllers = @[nav1,nav2,nav3,nav4];
 
        [self.window setRootViewController:tabBarController];
        [self.window makeKeyAndVisible];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读