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())
}
}
网友评论