Swift/OC 设置根视图 (ios13。。。)

xcode: Version 11.3.1 (11C504)
iOS: 13
语言:Swift/Objective-C

资料参考链接

指定根视图,兼容iOS13以及iOS13之前的系统:

SceneDelegate.swift文件中修改

@available(iOS 13.0, *)
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.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).
        
        guard let winScene = (scene as? UIWindowScene) else { return }

        // Create the root view controller as needed
        let vc = LoginViewController()
        let nc = UINavigationController(rootViewController: vc)

        // Create the window. Be sure to use this initializer and not the frame one.
        let win = UIWindow(windowScene: winScene)
        win.rootViewController = nc
        win.makeKeyAndVisible()
        window = win
    }

AppDelegate.swift文件中修改

var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        
        let loginVC = LoginViewController()
        let navi = UINavigationController(rootViewController: loginVC)
        window?.rootViewController = navi
        window?.makeKeyAndVisible()
        
        return true
    }

2021年2月5号更新:
更新内容Objective-C版本。
Appdelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    CGRect mainRect = [UIScreen mainScreen].bounds;
    self.window = [[UIWindow alloc]initWithFrame:mainRect];
    
    MainViewController *mainVC = [[MainViewController alloc]init];
    UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
    
    self.window.rootViewController = naviVC;
    [self.window makeKeyAndVisible];
    
    return YES;
}

SceneDelegate

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions  API_AVAILABLE(ios(13.0)){
    // 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).
    
    
    UIWindowScene *windowScene = [[UIWindowScene alloc]initWithSession:session connectionOptions:connectionOptions];
    self.window = [[UIWindow alloc]initWithWindowScene:windowScene];
    
    MainViewController *mainVC = [[MainViewController alloc]init];
    UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
    
    self.window.rootViewController = naviVC;
    [self.window makeKeyAndVisible];
}

完美结束

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS13 项目中的SceneDelegate类有什么作用?自从Xcode11发布以来,当你使用新XCode创建一...
    乐Coding阅读 31,022评论 14 61
  • iOS13 项目中的SceneDelegate类有什么作用?自从Xcode11发布以来,当你使用新XCode创建一...
    代码移动工程师阅读 13,867评论 3 36
  • 今天我网购的毛毯跟被套二合为一的的被套收到了,我看了好像比图片上要薄,图片上看起来厚实一些。不过,图片跟实物肯定有...
    兰奕阅读 1,360评论 0 0
  • 今天来谈谈投资。 投资无处不在。我们手里的钱,除了用来日常消费,结余我们会投在股票、房地产、债券、基金、银行等各个...
    晓燕小姐姐阅读 3,793评论 1 2
  • 2019遇见Metlife
    Metlife雨丹阅读 1,175评论 0 0

友情链接更多精彩内容