SwiftUI中的NavigationBarTitle文本颜色
我正在尝试更改导航Bartitle颜色(而不是背景)。我尝试了两种解决方案,但我的标题文本颜色只有在向下滚动时才会改变。屏幕打开时不改变SwiftUI中的NavigationBarTitle文本颜色,swift,swiftui,navigationbar,Swift,Swiftui,Navigationbar,我正在尝试更改导航Bartitle颜色(而不是背景)。我尝试了两种解决方案,但我的标题文本颜色只有在向下滚动时才会改变。屏幕打开时不改变 您需要为大标题添加相同的标题 .background (NavigationConfigurator { nc in nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.blue] nc.navigationBar.largeTitleTextAttr
您需要为大标题添加相同的标题
.background (NavigationConfigurator { nc in
nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.blue]
nc.navigationBar.largeTitleTextAttributes = [.foregroundColor : UIColor.blue]
})
另一种方法是使用外观而不是配置器,但应该在视图的init
中执行(创建NavigationView
之前),如
您还可以使用更完整的方法:
init() {
let coloredAppearance = UINavigationBarAppearance()
// for inline displayMode
coloredAppearance.titleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.blue]
// for automatic and large displayMode
coloredAppearance.largeTitleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.black]
// or
// for inline displayMode
// coloredAppearance.titleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.blue]
// for automatic and large displayMode
// coloredAppearance.largeTitleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.black]
// to make everything work normally
UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
}
var body: some View {
NavigationView {
Text("Screnn")
.navigationBarTitle("Title", displayMode: .inline)
}
// Do not forget this
// that means only show one view at a time no matter what device I'm working
.navigationViewStyle(StackNavigationViewStyle())
}
结果如下:
谢谢,它成功了。我不知道largetitle代表不同的文本。
init() {
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
}
init() {
let coloredAppearance = UINavigationBarAppearance()
// for inline displayMode
coloredAppearance.titleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.blue]
// for automatic and large displayMode
coloredAppearance.largeTitleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.black]
// or
// for inline displayMode
// coloredAppearance.titleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.blue]
// for automatic and large displayMode
// coloredAppearance.largeTitleTextAttributes = [.font:UIFont.preferredFont(forTextStyle: .title1), NSAttributedString.Key.foregroundColor: UIColor.black]
// to make everything work normally
UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
}
var body: some View {
NavigationView {
Text("Screnn")
.navigationBarTitle("Title", displayMode: .inline)
}
// Do not forget this
// that means only show one view at a time no matter what device I'm working
.navigationViewStyle(StackNavigationViewStyle())
}