gpt4 book ai didi

SwiftUI 列表。如何将圆角矩形作为列表中选定行的指示器

转载 作者:行者123 更新时间:2023-12-03 21:09:57 26 4
gpt4 key购买 nike

我有一个由核心数据填充的列表的代码:

      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))
这会产生一个如下图所示的列表:
enter image description here
但我不希望所选元素成为那个硬矩形。我希望所选元素四舍五入,如下所示:
enter image description here
我曾尝试更换 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 }
})

关于SwiftUI 列表。如何将圆角矩形作为列表中选定行的指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64431599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com