ios - SwiftUI 更新导航栏标题颜色
问题描述
如何在 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
,因此视图控制器仍然具有有效.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())
}
}
推荐阅读
- css - Nuxt 中的条件样式表
- reactjs - React - 使用 JSON 存储数据
- plpgsql - 从客户端,这与 SELECT 完美配合并返回布尔值。但是,作为 Function 它不能作为 plpgsl
- reactjs - 当我使用句柄登录方法时,它会导致“超出最大更新深度”
- ios - iOS SwiftUI:子视图中的数据流流@ObservedObject
- c# - 如何发布到休息 api 并使用 c# 中的非结束流?
- node.js - 如何从 fs.statSync 存根 isFile
- mysql - 为什么在 MySQL 触发器查询中出现语法错误?
- php - 使用 where 子句获取加密数据
- python - 如何使用 Google 的 Universal Sentence Encoder 根据多个文档找到最相似的文档?