我目前正在尝试创建一个包含三个相邻Picker
视图的页面,HStack
如下所示:
我做了一个CustomPicker
视图,将框架限制为 90 x 240,然后使用.compositingGroup()
和.clipped()
使每个选择器的可选区域不重叠。
CustomPicker.swift
import SwiftUI
struct CustomPicker: View {
@Binding var selection: Int
let pickerColor: Color
var numbers: some View {
ForEach(0...100, id: \.self) { num in
Text("\(num)")
.bold()
}
}
var stroke: some View {
RoundedRectangle(cornerRadius: 16)
.stroke(lineWidth: 2)
}
var backgroundColor: some View {
pickerColor
.opacity(0.25)
}
var body: some View {
Picker("Numbers", selection: $selection) {
numbers
}
.frame(width: 90, height: 240)
.compositingGroup()
.clipped()
.pickerStyle(.wheel)
.overlay(stroke)
.background(backgroundColor)
.cornerRadius(16)
}
}
ChoicePage.swift
struct ChoicePage: View {
@State var choiceA: Int = 0
@State var choiceB: Int = 0
@State var choiceC: Int = 0
var body: some View {
HStack(spacing: 18) {
CustomPicker(selection: $choiceA, pickerColor: .red)
CustomPicker(selection: $choiceB, pickerColor: .green)
CustomPicker(selection: $choiceC, pickerColor: .blue)
}
}
}
CustomPicker
在预览画布和模拟器中进行测试时ChoicePage
,它工作得非常好,但是当我尝试在我的物理设备(iPhone 8 和 iPhone 13,都在 iOS 15.1 上)上使用它时,可点击区域重叠。我已经尝试了这篇文章和这篇文章以及许多其他文章的解决方案,但似乎没有什么对我有用。