【问题标题】:How to create spacing between items in a SwiftUI List view?如何在 SwiftUI 列表视图中创建项目之间的间距?
【发布时间】:2019-12-30 09:50:41
【问题描述】:

我似乎找不到在List 项目之间创建间距的方法。 也许我一开始就不应该列清单?

你怎么看?

这是生成视图的代码:

struct ContentView: View {
    
    @ObservedObject var ratesVM = RatesViewModel()
    
    init() {
        UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        UITableView.appearance().backgroundColor = UIColor.init(named: "midnight blue")
        UITableViewCell.appearance().backgroundColor = .green
        UITableView.appearance().separatorStyle = .none
    }
    
    var body: some View {
        
        NavigationView {
            List (ratesVM.ratesList, id: \.self) { rate in
                    Image(systemName: "plus")
                    
                    Button(action: {print("pressed")}) {
                        Text(rate.hebrewCurrencyName)
                }
            }
            .navigationBarTitle("המרות מטבע")
            .navigationBarItems(leading:
                Button(action: {
                    print("button pressed")
                    self.ratesVM.callService()
                }) {
                    Image(systemName: "plus")
                        .foregroundColor(Color.orange)})
        }.environment(\.layoutDirection, .rightToLeft)
    }
}

【问题讨论】:

标签: ios swift listview swiftui


【解决方案1】:

您可以将最小列表行高定义为更大,这样行之间的间隔就会更大。

List (ratesVM.ratesList, id: \.self) { rate in
       Image(systemName: "plus")
       Button(action: {print("pressed")}) {
                    Text(rate.hebrewCurrencyName)
       }
}.environment(\.defaultMinListRowHeight, 50) //minimum row height

或者,您可以将行构建为 HStack 并指定框架高度。

List (ratesVM.ratesList, id: \.self) { rate in
    HStack {
       Image(systemName: "plus")
       Button(action: {print("pressed")}) {
          Text(rate.hebrewCurrencyName)
       }
    }.frame(height: 50) //your row height 
}.environment(\.defaultMinListRowHeight, 20) 

或作为 VStack 并使用 Spacers

List (ratesVM.ratesList, id: \.self) { rate in
    VStack{
        Spacer()
        HStack {
           Image(systemName: "plus")
           Button(action: {print("pressed")}) {
              Text(rate.hebrewCurrencyName)
           }
        }
        Spacer()
    }.frame(height: 50)
}.environment(\.defaultMinListRowHeight, 20) 

【讨论】:

  • 如果要删除间距,则无法正常工作。这只会设置单元格的最小高度,但不会调整它们之间的间距。
【解决方案2】:

SwiftUI 允许我们使用 padding() 修饰符在视图周围设置单独的填充。如果你在不带参数的情况下使用它,你会得到所有边的系统默认填充,像这样:

VStack {
    Text("SwiftUI")
        .padding()
    Text("rocks")
}

但您也可以自定义应用多少填充以及应用位置。因此,您可能只想将系统填充应用于一侧:

Text("SwiftUI")
    .padding(.bottom)

或者您可能想要控制对所有边应用多少填充:

Text("SwiftUI")
    .padding(100)

或者您可以将两者结合起来,在视图的一侧添加特定数量的填充:

Text("SwiftUI")
    .padding(.bottom, 100)

这样你就可以了

Text(rate.hebrewCurrencyName)
      .padding(50)

【讨论】:

    【解决方案3】:

    这里的快速解决方案是将相应的listStyle 设置为SidebarListStyle。例如:

    List {
        Text("First row")
        Text("Second row")
        Text("Third row")
    }
    .listStyle(SidebarListStyle())
    

    但请注意,在 macOS 和 iOS 上,侧边栏列表样式还在部分标题中显示披露指示符,允许用户折叠和展开部分。

    另一种快速的替代方法是使用Divider() 辅助视图(即,可用于分隔其他内容的视觉元素):

    List {
        Text("First row")
        Divider()
        Text("Second row")
        Divider()
        Text("Third row")
    }
    

    两种解决方案都不允许直接控制垂直间距,但至少提供了更宽敞的布局选择。

    【讨论】:

      【解决方案4】:

      填充不起作用,因为它只会增加项目/单元格的大小而不是之间的间距,但是 .environment(.defaultMinListRowHeight, 20) 似乎有效

      我还为按钮样式实现了一个自定义视图,以调整按钮相对于项目/单元格的框架和“可按下区域”。

      struct MyButtonStyle: ButtonStyle {
          func makeBody(configuration: Configuration) -> some View {
              configuration.label
                  .foregroundColor(.black)
                  .opacity(configuration.isPressed ? 0.5 : 1.0)
                  .frame(width: 350, height: 50, alignment: Alignment.center)
                  .background(Color.orange)
          }
      }
      
      struct ContentView: View {
      
          @ObservedObject var ratesVM = RatesViewModel()
      
          init() {
              UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.white]
              UITableView.appearance().backgroundColor = UIColor.init(named: "midnight blue")
              UITableViewCell.appearance().backgroundColor = .clear
              UITableView.appearance().separatorStyle = .none
          }
      
          var body: some View {
      
              NavigationView {
                  List (ratesVM.ratesList, id: \.self) { rate in
                      Button(action: {print("pressed")}) {
                          Text(rate.hebrewCurrencyName)
                      }.buttonStyle(MyButtonStyle())
                  }
                  .navigationBarTitle("המרות מטבע")
                  .navigationBarItems(leading:
                      Button(action: {
                          print("button pressed")
                          self.ratesVM.callService()
                      }) {
                          Image(systemName: "plus")
                              .foregroundColor(Color.orange)})
              }.environment(\.layoutDirection, .rightToLeft)
                  .environment(\.defaultMinListRowHeight, 150)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-10-24
        • 1970-01-01
        • 2017-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 1970-01-01
        • 2017-01-04
        相关资源
        最近更新 更多