目的
Storyboardを使わずにUIを作成する場合、必要となるXCodeの初期設定について説明
流れ
①: プロジェクトを作成し、実行できることを確認(Storyboardあり)
②: Main.storyboard を削除
③: SceneDelegate.swift のコードを全てコメントアウト
④: MainInterface の設定を変更
⑤: info.plist の Storyboard Name を削除
⑥: ViewController のプログラム変更
⑦: 実行結果
①: プロジェクトを作成し、実行できることを確認(Storyboardあり)
新規でプロジェクトを作成し、「▶︎」ボタンを押して実行可能なことを確認する
②: Main.storyboard を削除
Main.storyboard上で右クリック → 「Delete」 → 「Move To Trash」
③: SceneDelegate.swift のコードを全てコメントアウト
SceneDelegate.swift のコードを全て選択して、「Command + /」 でコメントアウト
AppDelegate.swift のコードを変更
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // アプリ起動時に表示する View を格納する変数
// 起動時に実行
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = ViewController() // アプリ起動時に表示するViewを指定
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.
// }
}
④: MainInterface の設定を変更
画面左上にある「"アプリ名"」 → 「Target」 → 「General」 → Deployment Info の 「Main Interface」 → Main を削除(空欄にする)
⑤: info.plist の編集
「info.plist」 → 「Application Scene Manifest」 → 「Application Session Role」 → 「item 0(Default Configuration)」 → 「-」(項目にカーソルを合わせると出てくる)
※ SceneDelegate で画面を呼び出す時は、「item 0(Default Configuration)」を消すのではなく、「item 0(Default Configuration)」でいらないものを消す。いらないもの については今回省略します!
⑥: ViewController のプログラム変更
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
}
⑦: 実行結果
見事、StoryboardなしでUI作成を行えました!

参考サイト