全画面のナビゲーションバーの設定を一括で変更するには
UINavigationControllerのサブクラスを作って共通の設定を追加すると簡単です。
ナビゲーションバーの戻るボタンタイトルを全画面で消したい、背景色やタイトルなどの設定も一括で行いたいのであれば、
以下のように、そのアプリ専用のナビゲーションを実装して、それをUINavigationControllerの代わりに利用するだけ。
// MyNavigationController.swift import Foundation import UIKit final class MyNavigationController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() navigationBar.tintColor = .green // 戻るボタンなど、ナビゲーションバーアイテムの色 navigationBar.barTintColor = .white // ナビゲーションバー の背景色 let attributes: [NSAttributedString.Key: AnyObject] = [ NSAttributedString.Key.foregroundColor: UIColor.black, ] navigationBar.titleTextAttributes = attributes // ナビゲーションバー タイトルの指定 delegate = self } } extension MyNavigationController: UINavigationControllerDelegate { // NavigationControllerがViewControllerやNavigationItemを呼び出す直前に呼ばれるDelegateメソッド内に // 戻るボタンのタイトルを""にする指定を追加 func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) } }
呼び出す時は、
let viewController = UIViewController() let navigationController = MyNavigationController(rootViewController: viewController)
のように、UINavigationControllerからMyNavigationControllerに置き換えるだけ🤗