如何在SwiftUI中更改.navigationBarTitle字体?

Saj*_*jad 4 ios swift swiftui xcode11

我正在将SwiftUI与Xcode 11结合使用,我想NavigationBarTitle用以下代码行更改字体:

.navigationBarTitle (Text("Navigation Bar Title"), displayMode: .inline)
    .font(.subheadline)
Run Code Online (Sandbox Code Playgroud)

但是什么也没发生。有什么建议或意见吗?

Anj*_*iya 6

In SwiftUI, at this point we can not change the 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("TEST")
            .navigationBarTitle (Text("TEST"), displayMode: .inline)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

I hope it will help you. Thanks!!

  • 就我而言,这不适用于 iOS 16>。 (3认同)