LoginSignup
0
1

More than 1 year has passed since last update.

【SwiftUI】NavigationBarのカスタマイズ

Posted at

自分が設定に少し手間取ったので、メモとして残します。色々と調べた結果をまとめ、汎用化コードを設計。
これさえあれば、ナビゲーションバーのデザインは完璧。

関数の準備

func setupNavigationBar() {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    //    背景色
    appearance.backgroundColor = UIColor(red: 33/255, green: 73/255, blue: 125/255, alpha: 1)
    //    タイトル文字色
    appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
    UINavigationBar.appearance().compactAppearance = appearance
    //    遷移後の「<戻る」ボタンの色
    UINavigationBar.appearance().tintColor = .white
}

あとはinit()の際に読み込む

    init(){
        setupNavigationBar()
    }

補足

# インライン表示(ほぼ全てのアプリがインライン表示)
 .navigationBarTitleDisplayMode(.inline)
# ナビゲーションバーを一時的に消したいとき
 .navigationBarHidden(true)
あとはNavigationbarItemsでメニューを追加するだけ。
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