⚔️

UIKitの最小構成(Main, LaunchScreen storyboardなし)

2024/01/16に公開

構成

  • Main.storyboard/LaunchScreen.storyboard無し
  • AppDelegate/ SceneDelegate有り
  • UIフレームワークはUIKit

Info.plist

Lancu Screen -> Background Colorを設定する

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>UILaunchScreen</key>
  <dict>
    <key>UIColorName</key>
    <string>LaunchScreenBackground</string>
  </dict>
</dict>
</plist>

AppDelegate.swift

import UIKit

@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
  func application(
    _ application: UIApplication,
    configurationForConnecting connectingSceneSession: UISceneSession,
    options: UIScene.ConnectionOptions
  ) -> UISceneConfiguration {
    let config = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
    config.delegateClass = SceneDelegate.self
    return config
  }
}

SceneDelegate.swift

import UIKit

final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  var window: UIWindow?

  func scene(
    _ scene: UIScene,
    willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions
  ) {
    let windowScene = scene as! UIWindowScene
    self.window = UIWindow(windowScene: windowScene)
    self.window!.rootViewController = ViewController()
    self.window!.rootViewController!.view.backgroundColor = .white
    self.window!.rootViewController!.view.layer.borderColor = UIColor.red.cgColor
    self.window!.rootViewController!.view.layer.borderWidth = 10
    window!.makeKeyAndVisible()
  }
}

ViewController.swift

final class ViewController: UIViewController {
}

Discussion