はじめに
コードでViewを表示させるときにViewController
をUINavigationControllerに埋め込みさせたいときのメモ
Storyboardを使わないプロジェクトの設定をします
やり方はこちらの記事どうぞ.
https://qiita.com/Imael/items/e908aef1e6fc077f29fc
コードです
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: scene)
let vc = ViewController()
// NavigationControllerのルートビューにする
let nvc = UINavigationController(rootViewController: vc)
window.rootViewController = nvc
window.makeKeyAndVisible()
self.window = window
}
}
UINavigationControllerクラスのrootViewControllerイニシャライザーにViewContorollerを渡します!