Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ScrollView content fill its parent in SwiftUI

Tags:

swift

swiftui

I'd love to build a scrolling screen, so I wanted to embed it ScrollView. But I am not able to achieve it the view just shrinks to its compressed size. Let's say that I want the ScrollView to scroll vertically so I'd love the content to match scrollView's width. So I use such preview:

struct ScrollSubview_Previews : PreviewProvider {
    static var previews: some View {
        func textfield() -> some View {
            TextField(.constant("Text")).background(Color.red)
        }

        return Group {
            textfield()
            ScrollView {
                textfield()
            }
        }
    }
}

But it ends with result like this:

enter image description here

like image 742
olejnjak Avatar asked Jun 06 '19 19:06

olejnjak


1 Answers

A simple way for you, using frame(maxWidth: .infinity)

    ScrollView(.vertical) {
        VStack {
           ForEach(0..<100) {
              Text("Item \($0)")
          }
       }
       .frame(maxWidth: .infinity)
   }
like image 81
Hoang Nguyen Avatar answered Oct 29 '22 00:10

Hoang Nguyen