Search code examples
swiftuinavigationview

Adding a Subtitle under NavigationTitle


Let's say I have the following code:

struct SwiftUIView: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello")
                Text("World")
            }
            .navigationTitle("SwiftUI")
        }
    }
}

I'd like to add a smaller subtitle right under SwiftUI. I tried adding something like .navigationSubtitle("") but it doesn't exist. I also tried reading the documentation, and it does mention func navigationSubtitle(_ subtitle: Text) -> some View, but I'm just not sure how to add that to my code. Thanks in advance!


Solution

  • I ended up doing something different: instead of making "SwiftUI" a navigation title, I just put it inside a VStack with the rest of the body, like so:

    struct SwiftUIView: View {
    var body: some View {
        NavigationView {
                    VStack {
                        
                        //Header
                        VStack(alignment: .leading, spacing: 5) {
                            Text("SwiftUI")
                                .font(.system(size: 35, weight: .semibold, design: .default))
                            Text("Subtitle")
                        }
                        .padding()
                        .padding(.leading, -110) //I'm still not sure how to give it a leading alignment without hardcoding it
                        Divider()
                        Spacer()
                        
                        //Body
                        VStack {
                            Text("Hello")
                            Text("World")
                        }
                        Spacer()
                        
                        //Navbar title
                    }
            }
    }}
    

    Thank you all for the help regardless!