【问题标题】:Left- or Center-Justifying Multiline Section Header for List in SwiftUISwiftUI 中列表的左对齐或居中对齐多行部分标题
【发布时间】:2021-04-18 14:22:42
【问题描述】:

这是我的代码:

struct ContentView: View {

    var body: some View {
        Form {
            Section(header:
                VStack {
                    Text("Header line 1")
                    Text("This is header line 2")
            }) {
                List {
                    Text("Item 1")
                    Text("Item 2")
                    Text("Item 3")
                    Text("Item 4")
                }
            }
        }
    }
}

问题是我无法让节标题的两行都以相同的方式对齐或对齐。我希望它们都左对齐,但我也愿意让它们都以屏幕为中心。原来如此,对齐方式很奇怪,如下图:

任何建议将不胜感激。

【问题讨论】:

    标签: list swiftui header alignment sections


    【解决方案1】:

    添加 VStack 对齐

    Section(header:
        VStack(alignment: .leading) { //<< here alignment
            Text("Header line 1")
            Text("This is header line 2")
    }) 
    

    【讨论】:

      【解决方案2】:

      对于任何寻找如何使其居中的人来说,这对我有用。要删除自动大写,请添加 .textCase(nil)

      Section(header:
          HStack {
              Spacer()
              VStack {
                  Text("Header line 1")
                  Text("This is header line 2")
              }
              Spacer()
          }) {
      
      }
      .textCase(nil)
      

      【讨论】:

        猜你喜欢
        • 2015-07-25
        • 1970-01-01
        • 2015-07-18
        • 2014-05-12
        • 2019-10-01
        • 1970-01-01
        • 2023-01-26
        • 2017-01-16
        • 2014-04-30
        相关资源
        最近更新 更多