LoginSignup
0
2

More than 1 year has passed since last update.

【SwiftUI】 NavigationBar backgroud color

Last updated at Posted at 2022-07-31

基本的な実装

swif
import SwiftUI

struct ContentView: View {

    init(){
        //ナビゲーションバーの背景色の設定
        UINavigationBar.appearance().barTintColor = .white
    }

    var body: some View {

        NavigationView(){
            VStack(){
                NavigationLink(destination: ContentView()){
                    Text("ナビゲーションビュー")
                }
            }
            .navigationBarTitle("ナビゲーション", displayMode: .inline)
        }
    }
}

もう一つの方法

swift
import SwiftUI
import UIKit

struct NavigationConfigurator: UIViewControllerRepresentable {
    var configure: (UINavigationController) -> Void = { _ in }

    func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfigurator>) -> UIViewController {
        UIViewController()
    }
    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfigurator>) {
        if let nc = uiViewController.navigationController {
            self.configure(nc)
        }
    }

}

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Don't use .appearance()!")
            .navigationBarTitle("Try it!", displayMode: .inline)
            .background(NavigationConfigurator { nc in
                // UIKitNavigationBarのスタイルを変更するのと同じ方法でOK
                nc.navigationBar.barTintColor = .blue
                nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.white]
            })
        }
    }
}
0
2
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
2