Xcode创建不使用Storyboard的项目
上周一直在忙蓝牙相关的内容,这周开始进行项目的迁移,下面记录一下使用Xcode创建项目的流程
首先就是在Xcode里面创建一个新项目,这里要设置好项目的名称,使用的语言,以及项目使用的模版,这里先设置为使用Storyboard
之后就得到了一个项目,这个项目还是使用storyboard的项目,可以看到项目的工程文件有一个main.storyboard,如果这个时候启动项目,项目就会加载这个模版中的内容,如果不想使用这个,则可以删除掉这个main.storyboard,然后在项目中进行设置,即删除掉Deployment info中的main interface中的内容
后面打开项目的info.plist,删除掉下面这个管理项的item0中的key为Storyboard Name的条目
如果还有的话,删除掉项目info.plist中的Main storyboard file base name条目(一般情况下都没有了)
之后就可以开始项目的初始化了,打开项目的SceneDelegate.swift文件,可以在scene函数中创建相应的window和Viewcontroller即可完成一个初始化页面的编辑
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow.init(frame: windowScene.coordinateSpace.bounds)
self.window?.windowScene = windowScene
self.window?.rootViewController = ViewController()
self.window?.makeKeyAndVisible()
}
//...
}
这样的话,就初步完成了项目的初始化,但是很多时候,我们需要设置我们的应用适配低版本的IOS应用,就需要设置项目的的TARGETS的Deployment info里面的IOS版本为10.0,设置Project的Deployment target里面的IOS支持版本为12.1
如此操作以后,我们的项目就不能使用UIScene进行项目初始化了,我么也需要修改项目代码,使得项目能够正常启动,首先需要注释了AppDelegate.swift中的两个函数,然后将window的初始化代码迁移到AppDelegate.swift中
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//如果要适配之前的机型的话,就不能使用SceneDelegate进行页面的创建和初始化了
//由于Xcode11以后将AppDelegate的生命周期函数和SceneDelegate做了关联,所以先要把下面两个函数注释
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = ViewController()
self.window?.makeKeyAndVisible()
return true
}
// MARK: UISceneSession Lifecycle
// func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// // Called when a new scene session is being created.
// // Use this method to select a configuration to create the new scene with.
// return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
// }
//
// func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// // Called when the user discards a scene session.
// // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
// }
}