LoginSignup
0
1

More than 1 year has passed since last update.

Storyboardを使わずにコードで画面を生成する

Last updated at Posted at 2021-08-10

はじめに

ストーリーボードを使わないアプリの設定方法を忘れてしまうので忘備録を作成

環境

Xcode Version 12.5.1 (12E507)

手順

1.アプリのDeployment Infoの設定

スクリーンショット 2021-08-10 16.34.34.png

Main Interfaceの”Main”の文字を削除

2.Info.plistのApplication Scene Manifestを項目ごと削除

スクリーンショット 2021-08-10 16.45.10.png

選択して項目ごと削除

3.不要なファイルの削除

SceneDelegate.swiftとMain.Storyboardは使わないので左のNavigatorから削除
※SceneDelegate.swiftは全文コメントアウトでも可

4.AppDelegate.swiftの編集

AppDelegate.swift
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    //①プロパティを宣言
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


        //②ViewControllerのClassをrootViewControllerに設定して表示する
        let window = UIWindow(frame: UIScreen.main.bounds)
        self.window = window
        window.makeKeyAndVisible()
        window.rootViewController = ViewController()

        //NavigationBarを使う場合
        //window.rootViewController = UINavigationController(rootViewController: ViewController())

        return true
    }

    // MARK: UISceneSession Lifecycle

    //③ 初めからある以下2つのメソッドをコメントアウト
//
//    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.
//    }

}

上記のように編集
①windowプロパティの宣言
②ViewControllerのClassをrootViewControllerに設定して表示する
③2つのメソッドをコメントアウト

参考にさせていただきました

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1