iosswiftswiftuixcode11

How change .navigationBarTitle font in SwiftUI?


I'm using SwiftUI with Xcode 11 and I want to change NavigationBarTitle font with these lines of codes:

.navigationBarTitle (Text("Navigation Bar Title"), displayMode: .inline)
    .font(.subheadline)

but nothing happened. any suggestion or comment?


Solution

  • 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)
            }
        }
    }
    

    I hope it will help you. Thanks!!