SwiftUI multiline text in a NavigationView Title
Asked Answered
N

3

25

How could I make this title multiline?

enter image description here

I have tried setting the title text as multiline or even configuring it with allowsThigtening(flag: Bool) but none of these options work.

Current code here:

import SwiftUI

struct DistributionCentersView: View {
    var dcViewModel = DistributionCenterViewModel()
    @State private var pickedSelectedData = 0
    @State private var searchTerm = ""

    init() {
        //...

    }

    var body: some View {
        NavigationView {
            VStack {
                VStack {
                    Picker(selection: $pickedSelectedData, label: Text("")){
                        Text("All").tag(0)
                        Text("Nevial").tag(1)
                        Text("Net power").tag(2)
                    }.pickerStyle(SegmentedPickerStyle())
                        .foregroundColor(Color.white)
                        .background(Color(#colorLiteral(red: 0.03921568627, green: 0.03921568627, blue: 0.03921568627, alpha: 1)))
                    SearchBar(text: $searchTerm)
                }
                .padding(.leading, 16)
                .padding(.trailing, 16)
                .padding(.top, 34)

                List {
                    ForEach (dcViewModel.distribuitionCenters){ dc in
                        ZStack { 
                            DistributionCenterCell()
                                .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                            NavigationLink(destination: Text("Test")) {
                                EmptyView()
                            }.buttonStyle(PlainButtonStyle())
                        }// This optionm avoid tinting
                    }.listRowBackground(Color(#colorLiteral(red: 0.03921568627, green: 0.03921568627, blue: 0.03921568627, alpha: 1)))
                }
                .navigationBarTitle(Text(Constants.Distribution.text), displayMode: .large)//Desired multiline title
                .listStyle(GroupedListStyle())
                .navigationBarItems(leading: NavigationLink(destination: ProfileView()) { Image(Constants.NavImages.human).foregroundColor(Color.white)}, trailing: Image(Constants.NavImages.phone).foregroundColor(Color.white))
            }
            .background(Color(#colorLiteral(red: 0.03921568627, green: 0.03921568627, blue: 0.03921568627, alpha: 1)))
        }

    }
}
Neurath answered 20/1, 2020 at 15:2 Comment(4)
Why not change the design of title?Ingemar
From design department, the title has to have two lines.Neurath
Can you please add your codeBanish
As far as I know standard UINavigationBar (which is currently used inside NavigationView) did not support multi-line text title ever. So if such is requirement you need custom title bar and use NavigationView for navigation purpose having hidden default navigation bar.Dissemble
C
20

For more information check this link

NavigationView {
    Text("Hello, SwiftUI!")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .principal) { 
                VStack {
                    Text("Title").font(.headline)
                    Text("Subtitle").font(.subheadline)
                }
            }
        }
}
Coloratura answered 20/7, 2021 at 10:15 Comment(1)
I like this answer, that's my need.Mayer
N
5

As Asperi has said, it is not possible to have a multiline navbar title by default. So, following Asperi suggestion, I have hidden the default title and I have set a custom Text():

VStack(alignment: .leading) {
                    Text(Constants.Distribution.text)
                    .font(.system(size: 34, weight: .heavy))
                    .foregroundColor(Color.white)
                }

. . .

.navigationBarTitle(Text(""), displayMode: .inline)
Neurath answered 21/1, 2020 at 7:33 Comment(3)
But this will break the native behavior of the title appearing in the nav bar when the user scrolls.Leftover
Dynamic type is also lost hereIntact
.font(.largeTitle.weight(.bold)) should be used insteadIntact
P
3

Modern, minimalistic approach

ToolbarItem(placement: .principal) { 
    Text("Title that will essentially span two lines").font(.headline)
        .fixedSize(horizontal: false, vertical: true)
}
Pringle answered 1/7, 2022 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.