0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

storyboardをなしで画面を呼び出す方法

Posted at

今回のゴールはこちら。
スクリーンショット 2021-09-10 16.34.04.png

プロジェクトを一から立ち上げてビルドした時に、Main.storyboardで白い画面が表示されると思うんですが、それをstoryboardなしで表示します。

やることはこちらの通り。

1.Main.Storyboardの削除
2.SceneDelegateを全てコメントアウト
3.AppDelegateの一部コメントアウト & 修正
4.Info.plistの一部分削除
5.PROJECTのGeneralの修正

それぞれ解説します。

1.Main.Storyboardの削除(画面を削除するだけなので、省きます)

2.SceneDelegateを全てコメントアウト(コメントアウトするだけなので、ここも省きます)

 
3.AppDelegateの一部コメントアウト & 修正
ソースはこちら。そのままコピー&ペーストでOKです。

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        
        window?.rootViewController = ViewController()
        
        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.
//    }


}

 
4.Info.plistの一部分削除
Application Scene Manifestの項目を全て削除してください。
スクリーンショット 2021-09-10 16.21.04.png

5.PROJECTのGeneralの修正

正確には、PROJECT → General → Deployment Info → MainInterFaceの削除

小さくてわかりづらいんですが、Mainとなっている部分を削除します。
スクリーンショット 2021-09-10 16.24.36.png
下記のように空白にします。
スクリーンショット 2021-09-10 16.31.40.png

そして最後にViewControllerのソースはこちら。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
    }
}
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?