UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.