xcode11新項目刪除main.storyboard 兩種方法


方法一

心急的童鞋按照老操作完成后再按照如下操作即可

 

/**
 棄用storboard
 1、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、啟動頁注釋
- (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項目后,想要刪除默認的main.storyboard,使用自定義的window和controller的坑。

具有一定經驗的人想必都知道Xcode11之前,想要達到上面的目的步驟吧。首先就是選中工程文件選項,之后刪除Main Interface選項里的Main,如下圖:

 

 
 

之后在Appdelegate的didFinishLaunchingWithOptions方法中自定義window並設置為keyWindow和讓它顯示,如下圖:

 
 

就這么簡單的實現了。

然鵝隨着iOS13的推出,在之前AppDelegate的基礎上多出了一個SceneDelegate,會將AppDelegate里的lifecycle的那些代理方法轉交給SceneDelegate,就是通過AppDelegate里以下兩個方法實現的

 

 
 

以下的內容是摘自蘋果官方文檔:

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不用你直接去創建對象,你可以用UIApplication里的requestSceneSessionActivation:userActivity:options:errorHandler:方法,這個方法會幫你初始化一個基於info.plist文件里的默認configuration的session對象。

 

 

 

因此xcode11中要實現自己的沒有默認main.storyboard的項目,就得將SceneDelegate里的lifecycle轉交給AppDelegate,按照上面所說,這一步操作就是,刪除或注釋一下截圖里的兩個方法

 
 

接近着刪除在info.plist里的Application Scene Manifest條目

 
 

之后就是xcode11以前的常規操作了,首先,刪除info.plist里的Main storyboard file base name條目

 
 

之后在AppDelegate.swift里添加window屬性,因為xcode默認刪除了這個屬性,現在你需要將它重新添加回來才行,如果是OC寫的話,就在AppDelegate.h里添加這個window屬性。

 
 

之后就在didFinishLaunch方法里初始化self.window,並設置為keywindow和讓它顯示,並初始化默認控制器即可。

 方法二
Xcode自動新增了一個SceneDelegate文件, 也就是說在iOS13中Appdelegate的作用發生了改變: iOS13之前,Appdelegate的作用是全權處理App生命周期和UI生命周期; iOS13之后,Appdelegate的作用是只處理 App 生命周期, 而UI的生命周期將全權由新增的SceneDelegate來處理.
 
SceneDelegate.m中的方法只有iOS13之后才能使用, 如果新建的項目要兼容之前的版本, 就需要自己在SceneDelegate和Appdelegate中做判斷
 

首先, UI相關的已經不能只放在Appdelegate中, 而是區分系統放在SceneDelegate中處理.

其次, 要在Info.plist中刪除對應的路徑.

 

最后, 在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];
}

 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM