繁体   English   中英

SwiftUI 中 ScrollView 下的按钮

[英]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)
            }
        }
    }
}
按钮和 3 个垂直布局的灰色矩形

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

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. 使滚动视图下的图像显示。
  2. 在 ScrollView 上制作一个不可见的按钮框架以使用它。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM