1

I am able to add Title using .navigationBarTitle(Text((msgDetails.name))) But i wanted to add subtitle under the title in the navigationbar. Looks like title will not accept the View and it accepts only Text. I tried \n in the title but it is not working. IS there any way i can add the subtitle in navigation bar. I used leading and trailing to add left and right button in the navigation bar. I wanted to show title and subtitle along with this left and right button

Navigation Bar

cto platform
  • 191
  • 3
  • 12

1 Answers1

2

If you look in SwiftUI documentation you'll see only a few overloads of navigationBarTitle function. All of them requires special parameters, like Text or StringProtocol. So you can't just put some View into navigation bar.

I can propose one strange, but working version. It's about using .navigationBarItems(leading:... - it requires some view, which you can customize (within reason). Here is simple example:

struct ContentView: View {

    var body: some View {

        NavigationView {
            Text("Main view")
                .navigationBarItems(leading:
                    HStack {

                        Button(action: {}) {
                            Image(systemName: "return")
                        }

                        VStack {
                            Text("Title")
                                .bold()
                                .font(.system(size: 30))

                            Text("Subtitle")
                                .italic()
                                .font(.system(size: 15))

                        }
                        .padding(.horizontal, 100) // mb it's better to use GeometryReader for centering

                })
        }


    }

}

and you'll achieve something like this:

enter image description here

Hrabovskyi Oleksandr
  • 3,070
  • 2
  • 17
  • 36