【问题标题】:Scrollable full screen view swift可滚动的全屏视图 swift
【发布时间】:2020-04-27 16:01:32
【问题描述】:

我希望在 Swift UI 中创建一个全屏可滚动视图。在将视图放入滚动视图和垂直堆栈后,我在让视图全屏显示时遇到问题?有什么建议?

这就是我的代码的样子

struct ContentView : View {
    var body: some View {

        //var posts = [Post(postColor: .red), Post(postColor: .blue), Post(postColor: .green)]
        ScrollView(.vertical, showsIndicators: false) {

            VStack {
                Post(postColor: .red)
                Post(postColor: .blue)
                Post(postColor: .green)

            }
        }
    }

}

struct Post : View {

    var postColor : Color

    var body : some View {

        VStack(alignment: .leading) {

                HStack {
                    Text("Name")
                        .font(.title)
                        .fontWeight(.heavy)

                    Spacer()
                   }

                Text("@Username")
                    .lineLimit(nil)
                    .font(.body)


                   Spacer()
                   }.background(postColor)

    }

}

图片 This is the output I have after embedding in a scrollview.

图片 I wish to have a full screen view like this but with the ability to scroll down to the next view(basically scroll down to next screen)

【问题讨论】:

    标签: ios xcode scrollview ios-simulator swiftui


    【解决方案1】:

    您可以使用GeometryReader 来实现:

    struct FullScreenViewsInScrollView: View {
        var body: some View {
    
            GeometryReader { geometry in
    
                ScrollView {
    
                    VStack(spacing: 0) {
                        Rectangle()
                            .foregroundColor(.red)
                            .frame(height: geometry.size.height)
    
                        Rectangle()
                            .foregroundColor(.blue)
                            .frame(height: geometry.size.height)
    
                        Rectangle()
                            .foregroundColor(.green)
                            .frame(height: geometry.size.height)
                    }
    
                }
    
            }
        }
    
    }
    

    结果应该是(我在实时预览中向下滚动了一点):

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2019-10-14
      相关资源
      最近更新 更多