剪切时SwiftUI选择器阻止输入
下面的代码示例将2行显示为hstack。 第二行包含一个裁剪为40 x 80的选择器 出于某种原因,它仍然在创建一个不可见的视图,这会阻止用户与第1行中的按钮进行交互 切换开关绘制在不可见视图上方,因此可以正常工作。但是按钮被画在下面,不起作用 如果交换切换和按钮的顺序,使按钮位于左侧,则按钮功能正常 这里的视频示例- 非常感谢任何能帮我解决这个问题的人剪切时SwiftUI选择器阻止输入,swiftui,Swiftui,下面的代码示例将2行显示为hstack。 第二行包含一个裁剪为40 x 80的选择器 出于某种原因,它仍然在创建一个不可见的视图,这会阻止用户与第1行中的按钮进行交互 切换开关绘制在不可见视图上方,因此可以正常工作。但是按钮被画在下面,不起作用 如果交换切换和按钮的顺序,使按钮位于左侧,则按钮功能正常 这里的视频示例- 非常感谢任何能帮我解决这个问题的人 import SwiftUI struct ContentView: View { @State private var
import SwiftUI
struct ContentView: View {
@State private var repSelection = 1
@State private var toggleOn = false
var body: some View {
VStack {
VStack {
//Row 1
//If the Button is on the right, it's covered by the invisible Picker view and doesn't work. If the button is on the left, it works.
//The Toggle seems to be drawn above the invisible view and works in either position
HStack {
Toggle("Switch", isOn: $toggleOn)
Button("Done"){ print("Tapped Done Button") }.padding().background(Color.gray)
}
//Row 2
HStack {
Spacer()
//For some reason, even though the Picker is clipped, it produces an invisible view that covers the button in Row 1,
Picker("Reps", selection:$repSelection){
ForEach(1...20, id:\.self) {
Text("\($0)")
}
}
.frame(maxWidth: 40, maxHeight: 80)
.compositingGroup()
.clipped()
.background(Color.green)
}.background(Color.purple)
}
}
}
}
struct PlaylistItemAdder_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是非常奇怪的行为。我猜即使你剪辑了选取器,背景在技术上仍然是绘制的,但不可见。无论如何,如果您在选择器的背景层上禁用命中测试,它似乎可以工作:
Picker("", selection:$repSelection){
ForEach(1...20, id:\.self) {
Text("\($0)")
}
}
.allowsHitTesting(true) // <-- allow clicking on picker
.frame(width: 40, height: 80)
.clipped()
.background(Color.green)
.allowsHitTesting(false) // <-- remove clicking on background
Picker(“,selection:$repSelection){
ForEach(1…20,id:\.self){
文本(“\($0)”)
}
}
.allowshitting(true)//非常感谢!