swift Navigation barの色を変更する
iOS13から新しいクラスが利用できる
こんなイメージ
UINavigationBarAppearanceを利用する
ios13以前は self.navigationController?.navigationBar.barTintColorを利用する
override func viewDidLoad() { super.viewDidLoad() self.title = "DiaryIndex" tableView.delegate = self tableView.dataSource = self setupNavigationBar() } func setupNavigationBar(){ self.navigationController?.navigationBar.prefersLargeTitles = true self.navigationController?.navigationBar.tintColor = UIColor.gray self.navigationController?.navigationBar.isTranslucent = false if #available(iOS 13.0, *) { let appearance = UINavigationBarAppearance() appearance.configureWithDefaultBackground() appearance.backgroundColor = UIColor.yellow appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray] appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray] navigationController?.navigationBar.standardAppearance = appearance navigationController?.navigationBar.scrollEdgeAppearance = appearance navigationController?.navigationBar.compactAppearance = appearance } else { self.navigationController?.navigationBar.barTintColor = UIColor(cgColor: CGColor(red: 252, green: 248, blue: 118, alpha: 1.0)) self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray] self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray] } }