iOS开发中,如果想使用纯代码方式设置窗口初始状态,可以删掉Main.StoryBoard,需要在AppDelegate中使用代码,并在Info.plist配置文件中将一些键值对删除,否则处理不当会出现崩溃或者黑屏。
相关操作如下:
1.在AppDelegate.h中声明一个UIWindow属性,在AppDelegate.m中实例化:
//AppDelegate.h @property (nonatomic, strong) UIWindow *myWindow; //AppDelegate.m CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); self.myWindow = [[UIWindow alloc] initWithFrame:frame]; [self.myWindow makeKeyAndVisible]; self.myWindow.backgroundColor = [UIColor whiteColor]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]]; self.myWindow.rootViewController = nav;
这里以UINavigationController作为UIWindow的根控制器为例。
2.Info.plist配置文件中删掉两个键值对:Application Scene Manifest和Main storyboard file base name