设置根视图

设置根视图,第1张

兼容iOS13以及iOS13之前的系统

1.工程配置

新建项目后,选中项目名称,在配置栏中选择Info栏目,在Custom iOS Target Properties子栏目中删除Main storyboard file base name项。

2.在AppDelegate.swift文件中修改

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 里面设置

	var window: UIWindow?
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        self.window?.backgroundColor = UIColor.yellow
        self.window?.makeKeyAndVisible()//设置这个window为keywindow
        self.window?.rootViewController = UINavigationController(rootViewController: OneViewController())//设置这个vc为navigation的根视图,设置这个navigation为这个Windows的根视图
        return true
    }
3.在SceneDelegate.swift文件中修改
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).
        if #available(iOS 13.0, *) {
            guard let winScene = (scene as? UIWindowScene) else { return }

                    // Create the root view controller as needed
                    let vc = OneViewController()
                    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
        }
    }
其他地方设置根视图
UIApplication.shared.keyWindow?.rootViewController =  BaseNavigationViewController(rootViewController: MyLoginViewController())
oc版本 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];
    
    OneViewController *mainVC = [[OneViewController 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];
    
    OneViewController *mainVC = [[OneViewController alloc]init];
    UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
    
    self.window.rootViewController = naviVC;
    [self.window makeKeyAndVisible];
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/994429.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存