如何在 SwiftUI 中更改导航栏标题的文本属性? [英] How do I change text attributes for a navigation bar title in SwiftUI?
问题描述
最近,我一直在研究新的 SwiftUI 框架.
lately, I've been playing around with the new SwiftUI framework.
我确实对该框架的工作原理有基本的了解,但我无法找到更改导航栏标题属性的方法.
I do have a basic understanding of how the framework works but I cannot figure out a way to change title attributes for a navigation bar.
我使用的是最新的 Xcode 11 beta 5 和相应的 swift/swiftUI 版本
I am using the latest Xcode 11 beta 5 and the corresponding swift/swiftUI version
我已经尝试了一些东西.我尝试的第一件事是向文本添加修饰符,但不知何故 SwiftUI 似乎忽略了导航栏环境中的修饰符.
I already tried a few things. First thing I tried was to just add modifiers to a Text, but somehow SwiftUI seems to ignore modifiers in a navigation bar environment.
我尝试的第二件事是通过 UINavigationBar.apperance().titleTextAttributes = ...
The second thing I tried was to change the title attributes through UINavigationBar.apperance().titleTextAttributes = ...
但似乎他们在最新的测试版中删除了自定义 UINavigationBar 的 titleTextAttributes 的功能.
but it seems like they removed the ability to customize titleTextAttributes of UINavigationBar in the latest beta version.
//First thing:
List(myData) {
...
}
.navigationBarTitle(Text("My title").font(myFont))
// Second thing
UINavigationBar.appearance().titleTextAttributes = myAttrs
我希望导航栏标题具有我的自定义字体,但它始终是苹果的默认字体.
I expect the navigation bar title to have my custom font but it's always the apple default font.
推荐答案
在 SwiftUI 中,此时我们无法直接更改 navigationBarTitle
字体,但您可以像这样更改导航栏外观,
In SwiftUI, at this point we can not change navigationBarTitle
font directly, but you can change navigationBar appearance like this,
struct YourView: View {
init() {
//Use this if NavigationBarTitle is with Large Font
UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
//Use this if NavigationBarTitle is with displayMode = .inline
//UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
}
var body: some View {
NavigationView {
Text("Hello World!")
.navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large)
//.navigationBarTitle (Text("Dashboard"), displayMode: .inline)
}
}
}
希望对你有帮助.谢谢!!
I hope this will help you. Thanks!!
这篇关于如何在 SwiftUI 中更改导航栏标题的文本属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!