首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SwiftUI中ScrollView下的按钮

SwiftUI中ScrollView下的按钮
EN

Stack Overflow用户
提问于 2019-12-08 23:15:23
回答 2查看 1.3K关注 0票数 5

我在Button的顶部有一个ScrollView,因为我想让ScrollView的内容在滚动时移动到按钮的顶部。然而,目前这意味着按钮不能工作,我猜想是因为scrollview接受了触摸事件。有没有办法让按钮在SwiftUI中正常工作?

代码语言:javascript
运行
复制
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)
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-12-09 20:28:46

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

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2020-05-15 22:37:59

  1. 在scrollView下制作要显示的图像。
  2. 在ScrollView上制作一个不可见的按钮框架以使用它。

1:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
Button(action: {

    }) {

}.frame(width: 100, height: 100)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59236793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档