Search code examples
swiftswiftuiswiftui-list

Remove empty space between section in SwiftUI


does anyone know of a way to remove the space between sections of a list in Swiftui? I tried inserting the footer as an EmptyView but it doesn't work. This problem occurs from iOS 16 onwards because on 15 it was enough to insert this line in the init: UITableView.appearance().sectionFooterHeight = 0

List {
     ForEach(servicesEnv.dictionary.keys.sorted(), id:\.self) { key in
            Section(header: SectionHeader(sectionName: "\(key)"),
                    footer: EmptyView()) {
                         MyView()
                            }
                        }
                    }
                }
            }
            .listStyle(GroupedListStyle())

Solution

  • Starting Xcode 15, iOS 17 / watchOS 10 (Still in Beta as of 03.08.2023) there are new view modifiers that allow you to set the spacing between sections to either a fixed value or variable ListSectionSpacing

    // Sets a fixed CGFloat value
    
    .listSectionSpacing(2)
    

    https://developer.apple.com/documentation/swiftui/view/listsectionspacing(_:)-a2sn

    // Sets a flexible ListSectionSpacing
    
    .listSectionSpacing(.compact)
    

    https://developer.apple.com/documentation/swiftui/view/listsectionspacing(_:)-5t518