0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but the spacing:0 removed a small gap, and the Spacer() after list raised it from the bottom a little, but the List still starts half way down the screen
Updated, how's that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.