Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI VStack without the space

I am trying to create a view which has a small form at the top with a text field. The results will display below, but I want the list to display directly under the form (with just a little gap) rather than half way down the screen.

I have tried adding Spacer() but cannot get the list to be directly under the form.

import SwiftUI

struct TestView: View {
    @State var location = ""
    
    var body: some View {
        VStack() {
            Form() {
                Text("Location")
                TextField("Location", text: $location)
                Button("Look up") {
                    // Lookup data
                }
            }
            /* Results will display in the list */
            List {
                Text("Location One")
                Text("Location Two")
                Text("Location Three")
                Text("Location Four")
                Text("Location Five")
                Text("Location Six")
            }
        }
    }
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can put the List inside the Form and separate them using Section. Plus no need for a VStack.

struct TestView: View {
  @State var location = ""

  var body: some View {
    Form {
      Text("Location")
      TextField("Location", text: $location)
      Button("Look up") {
        // Lookup data
      }
      Section {
        List {
          Text("Location One")
          Text("Location Two")
          Text("Location Three")
          Text("Location Four")
          Text("Location Five")
          Text("Location Six")
        }
      }
    }
  }
}

SwiftUI preview

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading