我有以下观点:
Swift 代码如下所示:
struct TestView: View {
let options = [" ", "1", "2", "3", "4", "5", "6"]
@State var selectedIndex: Int = 0
var body: some View {
HStack(spacing: 0) {
Text("One")
Spacer()
Picker(selection: $selectedIndex, label: Text(options[selectedIndex])) {
ForEach(0 ..< options.count) {
Text(options[$0])
}
}
.background(Color.red)
.pickerStyle(MenuPickerStyle())
}
.padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16))
.background(Color.yellow)
}
}
Run Code Online (Sandbox Code Playgroud)
当点击红色方块时,选择器将被打开:
如何扩展红色矩形的触摸区域以包括整个黄色区域?
@DonMag 的答案在 iOS 15 上停止工作。这是一个有效的更新答案。从技术上讲,它不使用Slider,但行为是相同的。Menu相反,使用a 。
struct PickerTestView: View {
let options = [" ", "1", "2", "3", "4", "5", "6"]
let optionNames = [" ", "One", "Two", "Three", "Four", "Five", "Six"]
@State var selectedIndex: Int = 0
var body: some View {
ZStack {
HStack(spacing: 0) {
Text(optionNames[selectedIndex])
Spacer()
}
.padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16))
.background(Color.yellow)
HStack(spacing: 0) {
Menu {
ForEach(0 ..< options.count) {
let index = $0
Button("\(options[index])") {
selectedIndex = index
}
}
} label: {
Label("", image: "")
.labelStyle(TitleOnlyLabelStyle())
.frame(maxWidth: .infinity)
}
}
}
}
}
struct PickerTestView_Previews: PreviewProvider {
static var previews: some View {
PickerTestView()
}
}
Run Code Online (Sandbox Code Playgroud)
让我们看看苹果什么时候决定打破这个实现。
| 归档时间: |
|
| 查看次数: |
1888 次 |
| 最近记录: |