SwiftUI 改变导航栏文字颜色和背景色

2020-04-02  本文已影响0人  fordG
import SwiftUI


/// 改变导航背景色和中间title文字颜色
/*
    NavigationView {
        Text("123")
        .navigationBarTitle(Text("title"), displayMode: .inline)
        .background(NavigationConfigurator(configure: { nav in
            nav.navigationBar.barTintColor = .white
            nav.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.purple]
        }))
    }.navigationViewStyle(StackNavigationViewStyle())
 */
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 MyNavtionView<Content> : View where Content : View  {
    @State var title: String
    var content: () -> Content
    var body: some View {
        NavigationView {
                content()
                .navigationBarTitle(Text(title), displayMode: .inline)
                .background(NavigationConfigurator(configure: { nav in
                    nav.navigationBar.barTintColor = .white
                    nav.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.purple, .font : UIFont.systemFont(ofSize: 18)]
                }))
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}
上一篇 下一篇

猜你喜欢

热点阅读