iOS开发之代码加载方式进入APP的界面

在iOS开发中,习惯使用代码的方式进入App的界面。这样给我的感觉更安心,也知道代码在做什么,是可控制的,而不是通过Info.plist的方式进入App里面。所以这里记录一下,方便以后查阅。

简述步骤:

* 创建Project

* 删除原有的ViewController的.m和.h文件。

* 删除storyborder文件。

* 删除Info.plist中关于main storyborder的配置。

* 创建新文件ViewController,勾选生成xib文件。

上面的步骤就完成了删除原有的启动方式,并且重新添加了ViewController文件。

最后需要在AppDelegate.m文件的didFinishLaunchingWithOptions方法中,添加相关启动代码:

如下:

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 2 
 3     //设置window属性,初始化windows的大小和位置
 4     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 5 
 6     //设置window的背景
 7     self.window.backgroundColor = [UIColor whiteColor];
 8 
 9     //初始化ViewController
10     ViewController *mainController=[[ViewController alloc]init];
11 
12     //设置此控制器为window的根控制器
13     self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:mainController];
14 
15     //定义导航条的颜色
16     [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:90.0/255 green:160.0/255 blue:240.0/255 alpha:1]];
17 
18 
19     CGFloat sysVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
20     if (sysVersion >= 8.0) {
21         //当系统版本低于8.0时,下面代码会引起App Crash
22         [[UINavigationBar appearance] setTranslucent:NO];
23     }
24 
25     //定义导航条上文字的颜色
26     [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
27 
28     //设置导航的标题的颜色
29     [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
30 
31     //设置window为应用程序主窗口并设为可见
32     [self.window makeKeyAndVisible];
33 
34     return YES;
35 }

接下来,要做的就是修改你的ViewController文件,在其xib文件中布局~

原文地址:https://www.cnblogs.com/vokie/p/4864239.html