LoginSignup
0
1

More than 3 years have passed since last update.

ソースコードでUINavigationController 2020summer

Last updated at Posted at 2020-08-02

Xcode 11.6
iOS 13.6

アプリの土台にUINavigationController(のサブクラス)を置くときのやりかた、のとりあえずのメモ。storyboardはさわらない。

  1. プロジェクト作成
  2. storyboardいじらず
  3. 最初に表示したいUIViewController(つまりUINavigationControllerの第1子)を作成。ここではMenuViewControllerという名前とする。
  4. 次のソースを書く。これは最初からあるViewControllerを上書きする。
class ViewController: UINavigationController {
    //ストーリーボードからこれが呼ばれる
    required init?(coder aDecoder: NSCoder) {
        super.init(rootViewController: MenuViewController())
    }
}

5.表示したいUIViewControllerのviewDidLoad()に

view.backgroundColor = UIColor.systemBackground//上部が黒くなるのを回避

を追加。

AppDelegateに書くやり方が主流

検索するとほとんどが以下のやり方である。

class AppDelegate: UIResponder, UIApplicationDelegate {

    //⭐️追加する
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //⭐️追加する
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = UINavigationController(rootViewController: MenuViewController())

        return true
    }

しかし、この記事のはじめに書いたやり方で何か不都合があるのだろうか?わからない。

backgroundColorの指定について

UINavigationControllerのviewに指定するのではなく傘下のUIViewControllerのviewで指定している理由
土台のclass ViewController: UINavigationControllerのviewにbackgroundColor = .systemBackgroundを指定すると、画面推移時にUINavigationController部分(戻るボタンなどが表示されるエリア)の描画がなぜか遅れて、色が不必要で微妙な変化をするから(iOS13)。

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