简体   繁体   English

如何从 Mac OS 中的 SwiftUI 列表中删除底部/顶部项目填充

[英]How to remove bottom/top item padding from SwiftUI List in Mac OS

I am having a hard time removing all padding from my cells in MacOS using SwiftUI.我很难使用 SwiftUI 从 MacOS 中的单元格中删除所有填充。 I can't seem to be able to do it even in Apple's Code!即使在 Apple 的代码中,我似乎也无法做到这一点!

https://developer.apple.com/tutorials/swiftui/creating-a-macos-app https://developer.apple.com/tutorials/swiftui/creating-a-macos-app

For example, inside the LandMarkList of the MacLandmarks folder in Xcode, I have put a .listRowInsets(EdgeInsets()) at the end of the forEach so that the code looks like this:例如,在 Xcode 中MacLandmarks文件夹的LandMarkList中,我在forEach的末尾放置了一个.listRowInsets(EdgeInsets()) ,这样代码如下所示:

struct LandmarkList: View {
    @EnvironmentObject private var userData: UserData
    @Binding var selectedLandmark: Landmark?
    @Binding var filter: FilterType

    var body: some View {
        List(selection: $selectedLandmark) {
            ForEach(userData.landmarks) { landmark in
                if (!self.userData.showFavoritesOnly || landmark.isFavorite)
                    && (self.filter == .all
                        || self.filter.category == landmark.category
                        || (self.filter.category == .featured && landmark.isFeatured)) {
                    LandmarkRow(landmark: landmark).tag(landmark)
                        .background(Color.red)
                }
            }
            .listRowInsets(EdgeInsets())
        }
    }
}

I have also put a red background color in each cell.我还在每个单元格中添加了红色背景色。 This is the result I am getting:这是我得到的结果:

顶部和底部的空间

The point is that I just can't seem to get rid of the vertical space between the cells of this list.关键是我似乎无法摆脱这个列表的单元格之间的垂直空间。 All solutions I have seen seem to mention iOS for this, but I want to do this in Mac OS (which should have the same behavior, but it doesn't).我见过的所有解决方案似乎都为此提到了 iOS,但我想在 Mac OS 中执行此操作(它应该具有相同的行为,但事实并非如此)。

Here is a demo of possible solution (tested with Xcode 11.4 / macOS 10.15.6).这是可能解决方案的演示(使用 Xcode 11.4 / macOS 10.15.6 测试)。

Note: if it will be needed to have several active Lists (ie. NSTableView) and some of them should have intercell spacing (aka separators) then they should be done by SwiftUI instruments, because this approach disables intercell spacing for all visible Lists注意:如果需要有多个活动列表(即 NSTableView)并且其中一些应该有单元间距(也称为分隔符),那么它们应该由 SwiftUI 仪器完成,因为这种方法会禁用所有可见列表的单元间距

演示

var body: some View {
    VStack {
        Text("Selected: \(selectedPerson ?? "<none>")")
        List(selection: $selectedPerson) {
             ForEach(persons, id: \.self) { person in
                Text(person)
             }
             .listRowBackground(Color.red)
             .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
        }.border(Color.green)
        .onReceive(NotificationCenter.default.publisher(for: NSView.frameDidChangeNotification)) {
            guard let tableView = $0.object as? NSTableView else { return }
            tableView.intercellSpacing = .zero
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM