iOS13啟動時黑屏

iOS13引進(jìn)了SceneDelegate,據(jù)說是為了ipad的多屏幕適配。我在像往常一樣創(chuàng)建self.window,并指定其tabbarController為window的rootViewController,發(fā)現(xiàn)屏幕黑屏。網(wǎng)上搜了一圈,發(fā)現(xiàn)自從引入了sceneDelegate,window的創(chuàng)建方式需要作出一些改變。

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];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容