苹果公司虽然大力推荐 storyboard,但是对于多数 iOS 开发者而言还是更喜欢纯代码开发。然而我们每次创建一个新的项目的时候,项目工程中都会自带一个 storyboard。要去除这个默认 storyboard 进行纯代码开发也并非难事,只要做以下几个步骤。
1.删除项目设置中的 Main interface
![](https://img.haomeiwen.com/i637119/ab382a95fb1676ab.jpg)
2.删除目录文件中的 Main.storyboard
![](https://img.haomeiwen.com/i637119/340ba978bf6b3ec4.jpg)
3.在AppDelegate 中添加项目的 window 并添加所需的 viewController
只需在didFinishLaunchingWithOptions中添加以下方法即可
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
self.window= [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// show root controller.
self.window.rootViewController= [[ViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
记得要引入对应的 viewController 头文件哟!
SWIFT 代码如下
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
return true
}
4.运行 App
运行app 之前最好能先清理生成文件夹,不让可能会出现运行失败的情况。清理生成文件夹的快捷键是 Shift+Command+Alt+K.
![](https://img.haomeiwen.com/i637119/119f976994425de3.png)
出现这个提示框后确认 Clean 。
网友评论