首页 > 解决方案 > SwiftUI MacOS 列表的 Inset 填充

问题描述

我在 macOS 应用程序上使用 SwiftUI 有一个滚动列表视图,我试图在可滚动区域内添加填充,但没有成功。这是我的代码:

List(appListItems, id: \.self, selection: $selectedItem) { app in
    ApplistItem(item: app, selected: selectedItem?.hashValue == app.hashValue)
}
.background(Color.red)
.padding(24)

但我得到的是:

在此处输入图像描述

设计需要的是这样,如您所见,我希望滚动条保持在右边缘,并且顶部/底部距离应包含在滚动区域中,而不是放在滚动区域之外。不需要水平填充,但我不知道如何处理顶部和底部填充并将它们保持在滚动区域内

在此处输入图像描述

标签: swiftmacosswiftui

解决方案


您需要列表行插图,但它适用于动态内容,因此您需要使用ForEach,例如

List {
   ForEach(appListItems, id: \.self, selection: $selectedItem) { app in
      ApplistItem(item: app, selected: selectedItem?.hashValue == app.hashValue)
   }
   .listRowInsets(EdgeInsets(top: 0, leading: 24, bottom: 0, trailing: 24))
}
.background(Color.red)

推荐阅读