我在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)
            }
        }
    }
}
发布于 2019-12-09 20:28:46
你必须把按钮放在最后,这样它就“在”滚动视图上了:
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()
            }
        }
    }
}发布于 2020-05-15 22:37:59
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)https://stackoverflow.com/questions/59236793
复制相似问题