Adding a Subtitle under NavigationTitle
Asked Answered
S

5

10

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!

Sicular answered 21/1, 2021 at 12:59 Comment(3)
Maybe this may be helpful for you? How can I add something like a “subheader” into the navigation bar in SwiftUI?Sabrinasabsay
check right side of the document : macOS 11.0+ Mac Catalyst 14.0+Blackfellow
@Bernhard, I saw that too. But is there a way to put it under the header instead of on top?Sicular
C
13

You can add a ToolbarItem with the principal placement:

struct SwiftUIView: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello")
                Text("World")
            }
            // .navigationTitle("SwiftUI") this won't make any changes now
            .toolbar {
                ToolbarItem(placement: .principal) {
                    VStack {
                        Text("title")
                        Text("subtitle")
                    }
                }
            }
        }
    }
}

The downside is that it overrides the navigation title, so any changes made with navigationTitle won't visible.

Colic answered 21/1, 2021 at 13:49 Comment(0)
I
4

Using a VStack in a toolbar causes the child view to display < Back for the the back navigation button rather than the title of the parent view. What I ended up doing is:

.navigationTitle("Title") // Will not be shown, but will be used for the back button of the child view
.toolbar {
    ToolbarItem(placement: .principal) {
        VStack {
            Text("Real Title").font(.headline)
            Text("Subtitle").font(.subheadline)
        }
    }
}
Imperishable answered 27/1, 2023 at 15:28 Comment(0)
M
2

You can do something like:

.navigationBarItems(leading:
    VStack(alignment: .leading, spacing: 5) {
        Text("SwiftUI")
            .font(.system(size: 35, weight: .semibold, design: .default))
        Text("Subtitle")
    }
 )
Maidie answered 18/5, 2021 at 14:12 Comment(0)
S
0

navigationSubtitle is not available on iOS. Only on macOS 11.0+ Mac Catalyst 14.0+ platform coded for.

Sinistrocular answered 14/1 at 3:57 Comment(0)
S
-1

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!

Sicular answered 21/1, 2021 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.