Ben*_*cob 6 button uiscrollview ios swift swiftui
我ScrollView在 a 之上有一个 a Button,因为我希望滚动时将 a 的内容ScrollView移动到按钮的顶部。然而,目前这意味着该按钮不起作用,我认为是因为滚动视图正在获取触摸事件。有什么方法可以让按钮在 SwiftUI 中工作吗?
struct Test: View {
@State var pressed:Bool = false
var body: some View {
ZStack {
VStack {
Button(action: {
self.pressed = true
}) {
Text("Button")
.padding(20)
.accentColor(pressed ? Color.green : Color.red)
}
Spacer()
}
ScrollView {
Spacer()
.frame(height:60)
Color.gray
.frame(height:200)
.padding(10)
Color.gray
.frame(height:200)
.padding(10)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*ris -2
您必须将按钮放在最后,以便它“位于”滚动视图上,如下所示:
struct ContentView: View {
@State var pressed:Bool = false
var body: some View {
ZStack {
ScrollView {
Spacer()
.frame(height:60)
Color.gray
.frame(height:200)
.padding(10)
Color.gray
.frame(height:200)
.padding(10)
Color.gray
.frame(height:200)
.padding(10)
Color.gray
.frame(height:200)
.padding(10)
Color.gray
.frame(height:200)
.padding(10)
Color.gray
.frame(height:200)
.padding(10)
}
VStack {
Button(action: {
self.pressed = true
print("tapped")
}) {
Text("Button")
.padding(20)
.accentColor(pressed ? Color.green : Color.red)
}
Spacer()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2485 次 |
| 最近记录: |