[英]Button under ScrollView in SwiftUI
我在Button
顶部有一个ScrollView
,因为我希望ScrollView
的内容在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)
}
}
}
}
你必须把按钮放在最后,这样它就像这样“超过”滚动视图:
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()
}
}
}
}
1:
VStack{
//Image Logo Start
Image("img_Logo")
.resizable()
.padding(.all, 10.0)
.frame(width: UIScreen.main.bounds.width * 0.4, height: UIScreen.main.bounds.height * 0.2)
//Image Logo Done
}
2:
Button(action: {
}) {
}.frame(width: 100, height: 100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.