gpt4 book ai didi

ios - SwiftUI 更新导航栏标题颜色

转载 作者:技术小花猫 更新时间:2023-10-29 11:06:09 41 4
gpt4 key购买 nike

如何在 SwiftUI 中更改导航栏标题颜色

NavigationView {
List{
ForEach(0..<15) { item in
HStack {
Text("Apple")
.font(.headline)
.fontWeight(.medium)
.color(.orange)
.lineLimit(1)
.multilineTextAlignment(.center)
.padding(.leading)
.frame(width: 125, height: nil)


Text("Apple Infinite Loop. Address: One Infinite Loop Cupertino, CA 95014 (408) 606-5775 ")
.font(.subheadline)
.fontWeight(.regular)
.multilineTextAlignment(.leading)
.lineLimit(nil)


}
}
}
.navigationBarTitle(Text("TEST")).navigationBarHidden(false).foregroundColor(.orange)
}

我试过 .foregroundColor(.orange) 但它不起作用

还尝试了 .navigationBarTitle(Text("TEST").color(.orange))

有什么帮助吗?

最佳答案

不需要使用 .appearance() 全局执行此操作。

虽然 SwiftUI 不直接公开导航样式,但您可以使用 UIViewControllerRepresentable 解决这个问题。由于 SwiftUI 在幕后使用常规 UINavigationController,因此 View Controller 仍将具有有效的 .navigationController 属性。

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 {
ScrollView {
Text("Don't use .appearance()!")
}
.navigationBarTitle("Try it!", displayMode: .inline)
.background(NavigationConfigurator { nc in
nc.navigationBar.barTintColor = .blue
nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.white]
})
}
.navigationViewStyle(StackNavigationViewStyle())
}
}

Modified navigation bar

关于ios - SwiftUI 更新导航栏标题颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56505528/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com