今年的Xcode11無疑是近幾年變化最大之一的一個版本了。我也在升級Xcode11后各種踩坑,爽到淚水止不住流啊。
這里就來說說我這兩天踩到的一個坑吧,就是用xcode11新建iOS項目后,想要刪除默認的main.storyboard,使用自定義的window和controller的坑。
具有一定經(jīng)驗的人想必都知道Xcode11之前,想要達到上面的目的步驟吧。首先就是選中工程文件選項,之后刪除Main Interface選項里的Main,如下圖:
之后在Appdelegate的didFinishLaunchingWithOptions方法中自定義window并設(shè)置為keyWindow和讓它顯示,如下圖:
就這么簡單的實現(xiàn)了。
然鵝隨著iOS13的推出,在之前AppDelegate的基礎(chǔ)上多出了一個SceneDelegate,會將AppDelegate里的lifecycle的那些代理方法轉(zhuǎn)交給SceneDelegate,就是通過AppDelegate里以下兩個方法實現(xiàn)的
以下的內(nèi)容是摘自蘋果官方文檔:
Overview
A?UISceneSession?object manages a unique runtime instance of your scene. When the user adds a new scene to your app, or when you request one programmatically, the system creates a session object to track that scene. The session contains a unique identifier and the configuration details of the scene. UIKit maintains the session information for the lifetime of the scene itself, destroying the session in response to the user closing the scene in the app switcher.?
You do not create session objects directly. UIKit creates sessions in response to user interactions with your app. You can also ask UIKit to create a new scene and session programmatically by calling the?requestSceneSessionActivation:userActivity:options:errorHandler:?method of?UIApplication. UIKit initializes the session with default configuration data based on the contents of your app's?Info.plist?file.
大概意思就是,一個UISceneSession不用你直接去創(chuàng)建對象,你可以用UIApplication里的requestSceneSessionActivation:userActivity:options:errorHandler:方法,這個方法會幫你初始化一個基于info.plist文件里的默認configuration的session對象。
因此xcode11中要實現(xiàn)自己的沒有默認main.storyboard的項目,就得將SceneDelegate里的lifecycle轉(zhuǎn)交給AppDelegate,按照上面所說,這一步操作就是,刪除或注釋一下截圖里的兩個方法
接近著刪除在info.plist里的Application Scene Manifest條目
之后就是xcode11以前的常規(guī)操作了,首先,刪除info.plist里的Main storyboard file base name條目
之后在AppDelegate.swift里添加window屬性,因為xcode默認刪除了這個屬性,現(xiàn)在你需要將它重新添加回來才行,如果是OC寫的話,就在AppDelegate.h里添加這個window屬性。
之后就在didFinishLaunch方法里初始化self.window,并設(shè)置為keywindow和讓它顯示,并初始化默認控制器即可。
至此,所有操作就完成了,你可以試試能否成功運行了