WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Removing the top padding of a List

Forums > SwiftUI

Hello,

I have a view with a TextField, and a List below, like this:

struct ContentView: View {
    var body: some View {
        NavigationStack {
            MainView()
        }
 }
}

struct ListView: View {
    var body: some View {
    List {
     Text("Title")
     }
  }
}

struct MainView: View {
    @State private var searchText = ""

    var body: some View {
        VStack {
            TextField("Search", text: $searchText)
                .textFieldStyle(RoundedBorderTextFieldStyle())
            ListView()
        }
    }
}

I am trying to move the list closer up to the textfield, but I can't get it to work. I don't want to use a "plain" list, because I'd still like to have the rounded corners and the horizontal padding.

Using padding or offset moves the list, but with the padding of the list itself, so it blocks the textfield. I have tried setting the spacing of the VStack to 0, but it doesn't change anything, and neither does applying any of the following modifiers to the list:

 .onAppear {
 UITableView.appearance().contentInset.top = -35
 }

.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))

.scrollContentBackground(.hidden)

.environment(\.defaultMinListHeaderHeight, 0)

Does anyone know how to do this? Thanks

1      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.