Swift 快捷菜单列表。如何制作圆形矩形作为列表中选定行的指示器
我有一个由核心数据填充的列表的代码:Swift 快捷菜单列表。如何制作圆形矩形作为列表中选定行的指示器,swift,swiftui,Swift,Swiftui,我有一个由核心数据填充的列表的代码: List { ForEach(items.filter { self.searchText.isEmpty ? true : $0.term!.contains(self.searchText) }, id: \.self) { item in Button(action: { globalVariable
List {
ForEach(items.filter {
self.searchText.isEmpty ? true : $0.term!.contains(self.searchText)
}, id: \.self) { item in
Button(action: {
globalVariables.selectedItem = item
}) {
Text(item.term!)
.font(fontItems)
.disabled(true)
.foregroundColor(globalVariables.selectedItem == item ? .black : .white)
}
.listRowBackground(
globalVariables.selectedItem == nil ? Color(UIColor.clear) : (
item == globalVariables.selectedItem ? Color("corListaSelecao") : Color(UIColor.clear)))
}
.onDelete(perform: deleteItems)
.onAppear{
globalVariables.selectedItem = items.first
}
.cornerRadius(20)
}
.background(Color("fundoControles"))
.cornerRadius(10)
.padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
这将生成一个类似下图的列表:
但我不希望所选元素是那个硬矩形。我希望所选元素四舍五入,如下所示:
我已尝试将listRowBackground
代码替换为
.listRowBackground(
RoundedRectangle(cornerRadius: 10)
.background(globalVariables.selectedItem == nil ? Color(UIColor.clear) : (
item == globalVariables.selectedItem ? Color.red : Color(UIColor.clear)))
)
注意:我已将矩形颜色更改为红色以说明问题
最终结果是在硬边橙色矩形上绘制一个红色圆形矩形。啊,红色矩形覆盖了文本
listRowBackground
不应该是背景吗?它如何覆盖文本
如何做我想做的事?这里是一个可能解决方案的演示。使用Xcode 12/iOS 14进行测试
.listRowBackground(Group {
if selectedItem == item {
Color.yellow.mask(RoundedRectangle(cornerRadius: 10))
} else { Color.clear }
})
mask
????????????明亮的为什么在这种情况下需要分组?