gpt4 book ai didi

ios - SwiftUI 中 ScrollView 下的按钮

转载 作者:行者123 更新时间:2023-12-04 11:48:03 27 4
gpt4 key购买 nike

我有一个 ScrollViewButton 之上因为我想要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)
}
}
}
}
Button and 3 gray rectangles vertically laid out

最佳答案

你必须把按钮放在最后,这样它就像这样“超过” ScrollView :

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()
}


}
}
}

关于ios - SwiftUI 中 ScrollView 下的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59236793/

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