如何正确制作此 Picker 剪辑
How do I make this Picker clip properly
我通过调用下面的视图在 HStack 中创建了一组并排选择器。每个拾取器的“控制”区域在拾取器左侧比应有的宽得多。 “.clipped()”应该可以解决这个问题,但它不起作用。在我开发代码的某个时候,它可以正常工作,但现在我无法取回它。当我尝试操纵选择器时,我可以将最右边的选择器移动到右侧 3 列的任意位置。左栏根本无法操作。这是怎么回事?
struct SinglePickerView: View {
@Binding var note: Int
let notes: [String]
let width: CGFloat
var body: some View {
VStack {
ZStack (alignment: .center) {
RoundedRectangle(cornerRadius: 5)
.fill(Color(red: 192, green: 192, blue: 192))
.frame(width: width-10, height: 25)
Text("\(notes[note])")
.foregroundColor(.black)
}
Picker(selection: $note, label: Text("")) {
ForEach(0 ..< 72) { index in
Text("\(self.notes[index])")
.foregroundColor(.white)
.tag(index)
}
}
.labelsHidden()
.frame(width: width)
.clipped()
}
}
}
尝试如下所示移动父容器中的剪辑。测试 Xcode 12 / iOS 14.
struct SinglePickerView: View {
@Binding var note: Int
let notes: [String]
let width: CGFloat
var body: some View {
VStack {
ZStack (alignment: .center) {
RoundedRectangle(cornerRadius: 5)
.fill(Color(red: 192, green: 192, blue: 192))
.frame(width: width-10, height: 25)
Text("\(notes[note])")
.foregroundColor(.black)
}
Picker(selection: $note, label: Text("")) {
ForEach(0 ..< notes.count, id: \.self) { index in
Text("\(self.notes[index])")
.foregroundColor(.white)
.tag(index)
}
}
.labelsHidden()
}
.frame(width: width)
.contentShape(Rectangle())
.clipped()
}
}
struct TestStackOfPickers: View {
let notes = (1...10).map { String([=10=]) }
@State var note1 = 1
@State var note2 = 5
@State var note3 = 3
var body: some View {
HStack {
SinglePickerView(note: $note1, notes: notes, width: 80).border(Color.red)
SinglePickerView(note: $note2, notes: notes, width: 80).border(Color.red)
SinglePickerView(note: $note3, notes: notes, width: 80).border(Color.red)
}
}
}
我终于找到了真正答案的参考。下面的公式至少在 iOS 13.6 和 XCode 11.6 中有效。
Picker(selection: $note, label: Text("")) {
ForEach(0 ..< 72) { index in
Text("\(self.notes[index])")
.frame(width: self.width)
.foregroundColor(.white)
.tag(index)
}
}
.labelsHidden()
.frame(width: width)
.compositingGroup()
.clipped()
我通过调用下面的视图在 HStack 中创建了一组并排选择器。每个拾取器的“控制”区域在拾取器左侧比应有的宽得多。 “.clipped()”应该可以解决这个问题,但它不起作用。在我开发代码的某个时候,它可以正常工作,但现在我无法取回它。当我尝试操纵选择器时,我可以将最右边的选择器移动到右侧 3 列的任意位置。左栏根本无法操作。这是怎么回事?
struct SinglePickerView: View {
@Binding var note: Int
let notes: [String]
let width: CGFloat
var body: some View {
VStack {
ZStack (alignment: .center) {
RoundedRectangle(cornerRadius: 5)
.fill(Color(red: 192, green: 192, blue: 192))
.frame(width: width-10, height: 25)
Text("\(notes[note])")
.foregroundColor(.black)
}
Picker(selection: $note, label: Text("")) {
ForEach(0 ..< 72) { index in
Text("\(self.notes[index])")
.foregroundColor(.white)
.tag(index)
}
}
.labelsHidden()
.frame(width: width)
.clipped()
}
}
}
尝试如下所示移动父容器中的剪辑。测试 Xcode 12 / iOS 14.
struct SinglePickerView: View {
@Binding var note: Int
let notes: [String]
let width: CGFloat
var body: some View {
VStack {
ZStack (alignment: .center) {
RoundedRectangle(cornerRadius: 5)
.fill(Color(red: 192, green: 192, blue: 192))
.frame(width: width-10, height: 25)
Text("\(notes[note])")
.foregroundColor(.black)
}
Picker(selection: $note, label: Text("")) {
ForEach(0 ..< notes.count, id: \.self) { index in
Text("\(self.notes[index])")
.foregroundColor(.white)
.tag(index)
}
}
.labelsHidden()
}
.frame(width: width)
.contentShape(Rectangle())
.clipped()
}
}
struct TestStackOfPickers: View {
let notes = (1...10).map { String([=10=]) }
@State var note1 = 1
@State var note2 = 5
@State var note3 = 3
var body: some View {
HStack {
SinglePickerView(note: $note1, notes: notes, width: 80).border(Color.red)
SinglePickerView(note: $note2, notes: notes, width: 80).border(Color.red)
SinglePickerView(note: $note3, notes: notes, width: 80).border(Color.red)
}
}
}
我终于找到了真正答案的参考。下面的公式至少在 iOS 13.6 和 XCode 11.6 中有效。
Picker(selection: $note, label: Text("")) {
ForEach(0 ..< 72) { index in
Text("\(self.notes[index])")
.frame(width: self.width)
.foregroundColor(.white)
.tag(index)
}
}
.labelsHidden()
.frame(width: width)
.compositingGroup()
.clipped()