【发布时间】:2020-06-28 22:59:58
【问题描述】:
我目前正在为 MacOS 应用程序的 SwiftUI 列表制作自定义行模板。由于我没有找到删除/调整蓝色突出显示颜色的方法(我只找到了适用于 iOS 的解决方案),我制作了以下模板,该模板涵盖了蓝色部分,但在所需区域内“模仿”了它,看起来像这个:Screenshot。我设法通过使用负填充等来覆盖大部分高光颜色;但是,我就是无法摆脱项目顶部的一些蓝色像素(在屏幕截图中标记)。
我的代码如下所示:
行模板(简化版)
struct EntryRow: View {
var body: some View {
// Outer frame (white space with shadow)
ZStack(alignment: .topLeading) {
// Inner frame (actual content)
ZStack(alignment: .topLeading) {
.frame(minWidth: 0, maxWidth: .infinity)
.frame(minHeight: 0, maxHeight: .infinity)
.padding(.vertical, 0)
.background(self.model.selectedEntries.firstIndex(of: entry.id) != nil ? Color.blue : Color(NSColor.controlBackgroundColor))
// ???????? Changes color according to selection state of row
.overlay(
RoundedRectangle(cornerRadius: 4)
.stroke(Color.black, lineWidth: 0)
)
.clipShape(RoundedRectangle(cornerRadius: 4))
}.padding(15)
.background(Color(NSColor.controlBackgroundColor))
// ???????? Changes background color so that it corresponds to the list's background color (which achieves the white space between the items)
.shadow(color: Color(NSColor.tertiaryLabelColor), radius: 2)
}
}
列表
按如下方式加载行:
List(selection: self.$model.selectedEntries) {
ForEach(self.model.entries, id: \.id) { item in
EntryRow(entry: item)
.padding(-15)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.top, -13).padding(.horizontal, -4)
问题
有没有办法覆盖剩余的高亮颜色,或者 - 甚至更好 - 完全移除高亮颜色?不幸的是,进一步调整填充似乎没有帮助......(但是也许我错过了什么)
非常感谢。
更新
选择绑定如下所示(如果相关:
class UserDataViewModel: ObservableObject {
@Published var entries = [Entry]()
@Published var selectedEntries: Set<Int32> = Set<Int32>() {
didSet {
print(selectedEntries.count)
}
}
【问题讨论】: