如何在Mac OS中从SwiftUI列表中删除底部/顶部项目填充 [英] How to remove bottom/top item padding from SwiftUI List in Mac OS
问题描述
我很难使用SwiftUI从MacOS的单元格中删除所有填充.即使使用Apple的Code,我似乎也无法做到!
重点是,我似乎无法摆脱此列表的单元格之间的垂直空间.我见过的所有解决方案似乎都提到了iOS,但我想在Mac OS中做到这一点(应该具有相同的行为,但事实并非如此).
此处是可能解决方案的演示(已通过Xcode 11.4/macOS 10.15.6测试).
注意:如果需要具有多个活动列表(即NSTableView),并且其中一些应该具有单元格间距(aka分隔符),则应由SwiftUI仪器完成,因为此方法会禁用单元格间距对于所有可见列表
变量主体:某些视图{VStack {文字("Selected:\(selectedPerson ??< none"")")列表(选择:$ selectedPerson){ForEach(persons,id:\ .self){文字(人)}.listRowBackground(Color.red).frame(maxWidth:.infinity,maxHeight:.infinity,对齐方式:.lead)} .border(颜色:绿色).onReceive(NotificationCenter.default.publisher(for:NSView.frameDidChangeNotification)){守护让tableView = $ 0.object as?NSTableView else {返回}tableView.intercellSpacing = .zero}}}
I am having a hard time removing all padding from my cells in MacOS using SwiftUI. I can't seem to be able to do it even in Apple's Code!
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:
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).
Here is a demo of possible solution (tested with 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
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
}
}
}
这篇关于如何在Mac OS中从SwiftUI列表中删除底部/顶部项目填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!