美文网首页
SwiftUI 改变导航栏文字颜色和背景色

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

作者: fordG | 来源:发表于2020-04-02 10:25 被阅读0次
    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())
        }
    }
    

    相关文章

      网友评论

          本文标题:SwiftUI 改变导航栏文字颜色和背景色

          本文链接:https://www.haomeiwen.com/subject/dhitphtx.html