🪬

アプリ全体にNavigationItemの色を反映させる

に公開

アプリケーション全体の NavigationBarItem の色を変更したい場合は、 AppDelegate に設定することで可能となります。

AppDelegate.swift
// ナビゲージョンアイテムの文字色
UINavigationBar.appearance().tintColor = UIColor.red

// ナビゲーションバーのタイトルの文字色
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.blue]

// ナビゲーションバーの背景色
UINavigationBar.appearance().barTintColor = UIColor.yellow

// ナビゲーションバーの背景の透過
(UINavigationBar.appearance() as UINavigationBar).setBackgroundImage(UIImage(), for: .default)

// ナビゲーションバーの下の影を無くす
UINavigationBar.appearance().shadowImage = UIImage()

アプリケーション全体ではなく、個別にカスタマイズする場合はカスタマイズしたい ViewController に指定することで可能となります。

カスタマイズしたいViewController
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // ナビゲージョンアイテムの文字色
    self.navigationController!.navigationBar.tintColor = UIColor.red
    // ナビゲーションバーのタイトルの文字色
    self.navigationController!.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.blue]
    // ナビゲーションバーの背景色
    self.navigationController!.navigationBar.barTintColor = UIColor.yellow
    // ナビゲーションバーの背景の透過
    self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
    // ナビゲーションバーの下の影を無くす
    self.navigationController!.navigationBar.shadowImage = UIImage()
}

参考記事

https://program-life.com/61

Discussion