0
0

More than 1 year has passed since last update.

【UIKit】iOS15でNavigationBarの色を変更する

Last updated at Posted at 2022-07-09

やり方

「Cocoa Touuch Class」を選択して「Next」を選択します。
スクリーンショット 2022-07-09 16.39.41.png

Class: 任意のクラス名を設定
Subclass of: UINavigationControllerを選択
スクリーンショット 2022-07-09 16.40.50.png

作成したクラスをStoryboardと紐付けます。
スクリーンショット 2022-07-09 16.45.50.png

作成したファイルに以下の記述をします

NavigationViewController.swift
import UIKit

class NavigationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.barTintColor = UIColor(red: 85/255, green: 197/255, blue: 3/255, alpha: 255/255)
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = .red
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().compactAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
        } else {
            navigationBar.barTintColor = .red
        }
    }

}

デフォルトカラーにする

Simulator Screen Shot - iPhone 12 - 2022-07-09 at 15.51.46.png

appearance.configureWithDefaultBackground()

色を選択する

Simulator Screen Shot - iPhone 12 - 2022-07-09 at 15.48.26.png

appearance.backgroundColor = .red
0
0
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
0