如何在SwiftUI中删除列表左右的填充[重复]

dy2hfwbg  于 2023-10-15  发布在  Swift
关注(0)|答案(2)|浏览(120)

此问题已在此处有答案

How to remove the left and right Padding of a List in SwiftUI?(10个答案)
三年前就关门了。
我正在使用List来动态创建一个视图的多个示例,但是,当使用列表时,填充被添加到子视图的左侧和右侧,我不能删除它。我试过使用.listRowInset,但没有产生明显的效果。
我已经创建了一个按需工作的PostView。我使用'PostsView'来动态生成多个PostView示例。
我试图让PostView中的图像接触PostsView中的屏幕边缘。

PostView

import SwiftUI


struct PostView: View {
    var post: Post

    var body: some View {

        VStack {
            HStack {
                Text(post.songInfo.album).font(.title)
                Spacer()
                Text(post.songInfo.artist)
            }.padding()

            Image(post.songInfo.albumArtUrl)
                .resizable()
                .aspectRatio(UIImage(named: "sweetner")!.size,  contentMode: .fit)
            HStack {
                Image("plus-icon")
                Spacer()
                Image("comment-icon")
                Spacer()
                Image("headset-icon")
            }.padding()

            HStack {
                Text(post.user.userName)
                Spacer()
                Text(post.textContent)
            }.padding()

        }
    }
}

PostsView

import SwiftUI

struct PostsView: View {
   @ObservedObject var fbVM = FirebaseInfo()

    var body: some View {

       List(fbVM.visiblePosts) { post in
            PostView(post: post)
       }

    }
}

struct PostsView_Previews: PreviewProvider {
    static var previews: some View {
        PostsView()
    }
}

kadbb459

kadbb4591#

更新

查看this answer
看起来.listRowInsets不适用于使用内容初始化的List中的行
所以只要稍微修改一下代码就可以做到这一点。首先,你需要从post视图中删除padding,这样文本就会移到边缘(如果你想这样做的话):

struct PostView: View {
  let placeholder = UIImage(named: "arianaGrande")!
  var post: Post

  var body: some View {
    VStack {
      HStack {
        Text(post.album).font(.title)
        Spacer()
        Text(post.artist)
      }// .padding() -- This padding adds space before "Sweetener and after Ariana Grande

      Image(uiImage: placeholder)
        .resizable()
        .aspectRatio(placeholder.size,  contentMode: .fit)
      HStack {
        Image("plus-icon")
        Spacer()
        Image("comment-icon")
        Spacer()
        Image("headset-icon")
      }.padding()

      HStack {
        Text(post.username)
        Spacer()
        Text(post.textContent)
      }//.padding() -- Same thing here.
    }
  }
}

struct PostView_Previews: PreviewProvider {
  static var previews: some View {
    PostView(post: Post(
      artist: "Ariana Grande",
      album: "Sweetener",
      albumArtUrl: "",
      username: "DoesData",
      textContent: "Text Content"
      )
    )
  }
}

然后,您可以稍微不同地初始化列表以删除空格。

struct PostsView: View {
  var posts = [
    Post(
      artist: "Ariana Grande",
      album: "Sweetener",
      albumArtUrl: "",
      username: "DoesData",
      textContent: "Text Content"
    )
  ]

  var body: some View {
    List {
      ForEach(posts) { post in
        PostView(post: post)
          .listRowInsets(EdgeInsets())
      }
    }
  }
}

struct Post: Identifiable {
  let id = UUID()
  let artist: String
  let album: String
  let albumArtUrl: String
  let username: String
  let textContent: String
}

struct PostsView_Previews: PreviewProvider {
  static var previews: some View {
    PostsView()
  }
}

测试结果:

原始答案

看起来你可以调整列表本身的padding来删除前导和尾随边距。

import SwiftUI

struct PostsView: View {
   @ObservedObject var fbVM = FirebaseInfo()

    var body: some View {

       List(fbVM.visiblePosts) { post in
            PostView(post: post)
       }.padding(.horizontal, -20) // -20 seems to make it go edge to edge

    }
}

struct PostsView_Previews: PreviewProvider {
    static var previews: some View {
        PostsView()
    }
}

您可能还需要调整PostView内部的填充,以使文本像这样Text("Title").font(.title).padding(.leading, -10)这似乎比它应该的更复杂,但它似乎工作。

bnl4lu3b

bnl4lu3b2#

列表中有几种间距可以更改。看看这个彩色编码的Map,并选择一个适合您的需求:

List(1...100, id: \.self) { item in
    Text("\(item)")
        .padding() // 🟣 comment to remove PURPLE padding
        .background(Color.yellow)
        // .listRowInsets(EdgeInsets()) // 🔵 uncomment to remove BLUE inset
}
// .listStyle(.plain) // 🟢 uncomment to remove GREEN inset
// .listStyle(.grouped) // 🔴 uncomment to remove RED inset

RTL-Demo

重要提示:

看起来.listRowInsets不适用于使用内容初始化的List中的行。因此,您需要在list中使用ForEach,如下所示:

var body: some View {
    List {
        ForEach(...) {
            Text("String")
                .listRowInsets(EdgeInsets())
        }
    }
}

相关问题