3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SwiftUI】NavigationView タイトルカスタマイズ方法

Last updated at Posted at 2020-08-20

タイトルとアクション等の設定

NavigationView に属性をつけるのではなく中身につけることに注意。

NavigationView {
    VStack {
        Text("How to customize view")
    }
    .navigationBarTitle(Text("Hello"))
    .navigationBarItems(trailing: Button("Done", action: {
        // Action
        // 左に作るときは leading: にする
    }))
    // .inline .automatic .large
    .navigationBarTitleDisplayMode(.automatic)
    .navigationBarBackButtonHidden(true)
}

タイトルのフォント/背景色の変え方

appearance()はあまり使わない方がいいという意見もありますが、これがとりあえず一番やりやすいかと思います。
UIViewController の属性を引き継いでいるので、中身はUIFont UIColor で設定します。

struct MyView: View {
    init() {
         // 背景を透明にする
         UINavigationBarAppearance().configureWithTransparentBackground()

         // コンパクトなナビゲーションバーの時の背景色を変える
         UINavigationBar.appearance().barTintColor = .systemRed

         // コンパクトなナビゲーションバーのタイトル属性
         UINavigationBar.appearance().titleTextAttributes = [
             .font: UIFont.systemFont(ofSize: 10)
             .foregroundColor: .systemBlue,
         ]
         UINavigationBar.appearance().largeTitleTextAttributes = [
             .font: UIFont.systemFont(ofSize: 20)
             .foregroundColor: .systemBlue,
         ]
     }
     var body: some View {
          // Navigation View
     }
}
3
4
2

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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?