简体   繁体   中英

Scrollable full screen view swift

I wish to create a full screen scrollable view in Swift UI. I am having a problem in getting the views to display full screen after putting them in a scrollview and vertical stack? Any suggestions?

This is how my code looks

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)

    }

}

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

Image 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)

You can use GeometryReader to achieve this:

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)
                }

            }

        }
    }

}

the result should be ( I scrolled down a little in the live preview ):

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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