xcode11新项目删除main.storyboard 两种方法
?
方法一
心急的童鞋按照老操作完成后再按照如下操作即可
?
?
/**棄用storboard1、info.plist去除<key>UIApplicationSceneManifest</key><dict><key>UIApplicationSupportsMultipleScenes</key><false/><key>UISceneConfigurations</key><dict><key>UIWindowSceneSessionRoleApplication</key><array><dict><key>UISceneConfigurationName</key><string>Default Configuration</string><key>UISceneDelegateClassName</key><string>SceneDelegate</string><key>UISceneStoryboardFile</key><string>Main</string></dict></array></dict></dict>2、啟動(dòng)頁(yè)注釋 - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {// Called when a new scene session is being created.// Use this method to select a configuration to create the new scene with.return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role]; }- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {// 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. }*/?
?
具體如下:
用xcode11新建iOS項(xiàng)目后,想要?jiǎng)h除默認(rèn)的main.storyboard,使用自定義的window和controller的坑。
具有一定經(jīng)驗(yàn)的人想必都知道Xcode11之前,想要達(dá)到上面的目的步驟吧。首先就是選中工程文件選項(xiàng),之后刪除Main Interface選項(xiàng)里的Main,如下圖:
?
?
?
之后在Appdelegate的didFinishLaunchingWithOptions方法中自定義window并設(shè)置為keyWindow和讓它顯示,如下圖:
?
?
就這么簡(jiǎn)單的實(shí)現(xiàn)了。
然鵝隨著iOS13的推出,在之前AppDelegate的基礎(chǔ)上多出了一個(gè)SceneDelegate,會(huì)將AppDelegate里的lifecycle的那些代理方法轉(zhuǎn)交給SceneDelegate,就是通過(guò)AppDelegate里以下兩個(gè)方法實(shí)現(xiàn)的
?
?
?
以下的內(nèi)容是摘自蘋(píng)果官方文檔:
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.
大概意思就是,一個(gè)UISceneSession不用你直接去創(chuàng)建對(duì)象,你可以用UIApplication里的requestSceneSessionActivation:userActivity:options:errorHandler:方法,這個(gè)方法會(huì)幫你初始化一個(gè)基于info.plist文件里的默認(rèn)configuration的session對(duì)象。
?
?
?
因此xcode11中要實(shí)現(xiàn)自己的沒(méi)有默認(rèn)main.storyboard的項(xiàng)目,就得將SceneDelegate里的lifecycle轉(zhuǎn)交給AppDelegate,按照上面所說(shuō),這一步操作就是,刪除或注釋一下截圖里的兩個(gè)方法
?
?
接近著刪除在info.plist里的Application Scene Manifest條目
?
?
之后就是xcode11以前的常規(guī)操作了,首先,刪除info.plist里的Main storyboard file base name條目
?
?
之后在AppDelegate.swift里添加window屬性,因?yàn)閤code默認(rèn)刪除了這個(gè)屬性,現(xiàn)在你需要將它重新添加回來(lái)才行,如果是OC寫(xiě)的話,就在AppDelegate.h里添加這個(gè)window屬性。
?
?
之后就在didFinishLaunch方法里初始化self.window,并設(shè)置為keywindow和讓它顯示,并初始化默認(rèn)控制器即可。
?方法二
Xcode自動(dòng)新增了一個(gè)SceneDelegate文件, 也就是說(shuō)在iOS13中Appdelegate的作用發(fā)生了改變: iOS13之前,Appdelegate的作用是全權(quán)處理App生命周期和UI生命周期; iOS13之后,Appdelegate的作用是只處理 App 生命周期, 而UI的生命周期將全權(quán)由新增的SceneDelegate來(lái)處理.
?
SceneDelegate.m中的方法只有iOS13之后才能使用, 如果新建的項(xiàng)目要兼容之前的版本, 就需要自己在SceneDelegate和Appdelegate中做判斷
?
首先, UI相關(guān)的已經(jīng)不能只放在Appdelegate中, 而是區(qū)分系統(tǒng)放在SceneDelegate中處理.
其次, 要在Info.plist中刪除對(duì)應(yīng)的路徑.
?
最后, 在SceneDelegate.m中添加根控制器
?
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];UIViewController *rootVc = [[UIViewController alloc]init];rootVc.view.backgroundColor = [UIColor purpleColor];UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:rootVc];[self.window setRootViewController:rootNav];[self.window makeKeyAndVisible]; }?
?
?
簽 名:被別人嫉妒,說(shuō)明你卓越;你嫉妒別人,說(shuō)明你無(wú)能。 座右銘:抓緊一切時(shí)間睡覺(jué)。
總結(jié)
以上是生活随笔為你收集整理的xcode11新项目删除main.storyboard 两种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ubantu 解决软件商店打不开问题
- 下一篇: storyboard 使用