NavigationView标题中的SwiftUI多行文字 [英] SwiftUI multiline text in a NavigationView Title

查看:164
本文介绍了NavigationView标题中的SwiftUI多行文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何使标题多行显示?

How could I make this title multiline?

我尝试将标题文本设置为多行,甚至使用allowThigtening(flag:Bool)进行配置,但这些选项均无效.

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

此处的当前代码:

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

    }
}

谢谢

推荐答案

正如Asperi所说,默认情况下不可能有多行导航栏标题.因此,按照Asperi的建议,我隐藏了默认标题,并设置了自定义Text():

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)

这篇关于NavigationView标题中的SwiftUI多行文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆