gpt4 book ai didi

ios - SwiftUI Picker 手动触发扩展

转载 作者:行者123 更新时间:2023-12-05 00:23:20 27 4
gpt4 key购买 nike

我有以下观点:
enter image description here
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)
}
}
单击红色方 block 时,将打开 Picker:
enter image description here
如何扩展红色矩形的触摸区域以包括整个黄色区域?

最佳答案

不确定这正是您所追求的,但试一试(初始 View 是“空白”黄色条):

import SwiftUI

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) {
Picker(selection: $selectedIndex, label: Text(" ").frame(maxWidth: .infinity), content: {
ForEach(0 ..< options.count) {
Text(options[$0])
}
})
.pickerStyle(MenuPickerStyle())
}
}
}

}

struct PickerTestView_Previews: PreviewProvider {
static var previews: some View {
PickerTestView()
}
}
启动时:
enter image description here
点击黄色栏上的任意位置:
enter image description here
选择“3”后:
enter image description here

关于ios - SwiftUI Picker 手动触发扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65549428/

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