美文网首页iOS 技术分享
iOS - 设置self.window.rootViewCont

iOS - 设置self.window.rootViewCont

作者: Joh蜗牛 | 来源:发表于2022-07-14 15:13 被阅读0次
一.在Xcode11.0之前新建的项目,设置根视图,代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    TabViewController *ltabVC = [[TabViewController alloc]init];
    ltabVC.delegate = self;
    self.window.rootViewController = ltabVC;
    [self.window makeKeyAndVisible];
    return YES;
}
二.在Xcode11.0之后新建的项目,项目会自动生成AppDelegate文件之外,还会生成SceneDelegate文件。

设置根视图代码如下:
(兼容iOS13和iOS13以前的项目AppDelegate和SceneDelegate类方法里设置)
1.在AppDelegate.h里加声明window:

@property (nonatomic, strong) UIWindow * window;

2.在AppDelegate.m设置:


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

3.在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).
          UIWindowScene *windowScene = (UIWindowScene *)scene;
          self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
          self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
          [self.window setWindowScene:windowScene];
          [self.window setBackgroundColor:[UIColor whiteColor]];
          [self.window setRootViewController:[UITabBarController new]];
          
          [self.window makeKeyAndVisible];
}
获取window
if (@available(iOS 13.0, *)) {
        window = [UIApplication sharedApplication].windows[0];
    } else {
        window = [UIApplication sharedApplication].delegate.window;
    }

相关文章

网友评论

    本文标题:iOS - 设置self.window.rootViewCont

    本文链接:https://www.haomeiwen.com/subject/zztfirtx.html