ios - 可快速滚动全屏 View

标签 ios xcode scrollview ios-simulator swiftui

我希望在 Swift UI 中创建全屏可 ScrollView 。将 View 放入 ScrollView 和垂直堆栈后,我在使 View 全屏显示时遇到问题吗?有什么建议么?

这就是我的代码的样子

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)

最佳答案

您可以使用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)
                }

            }

        }
    }

}

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

enter image description here

关于ios - 可快速滚动全屏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59674291/

相关文章:

iphone - 如何从pdf文件中的FileAttachment Annotation获取EmbeddedFiles

ios - 从 Plist 中检索颜色。 iOS

android - 按钮自带键盘我该如何修复

android - 是否可以使用 ScrollView 和 Horizo​​ntalScrollView 实现对角滚动?

php - NSDate 到 MySQL 日期

ios - 安装 Xcode 11 后无法在模拟器上运行应用程序 - CFBundleVersion 错误

ios - Swift 2.3 会与 iOS 10 兼容吗?

android - 在android中滚动线性布局

ios - 查找用户在 ScrollView 中拖动手指的 X 位置

javascript - React Native 0.56 : bundle doesn't produce . 元文件 - 还需要吗?