Is there a way how to set contentInset
on SwiftUI's List
? I need to adjust the bottom inset to have the last item visible above the bottom button.
My current solution is the following
Section(header: Color.clear.frame(height: 64)) { EmptyView() }
but I wonder is there a better way?
My solution to your problem looks similar with Asperi's answer but his answer will not show the Button
at the top of the view since the Button
is at the last section's footer in the Form
. (Button
will not be visible if you don't scroll to bottom.)
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme
let stringArray: [String] = [String](repeating: "Example", count: 30)
var body: some View {
ZStack(alignment: .bottom) {
List {
ForEach(0..<stringArray.count) { stringIndex in
Section {
Text(stringArray[stringIndex])
}
if stringIndex == stringArray.count - 1 {
Spacer()
.listRowInsets(EdgeInsets())
.listRowBackground(
colorScheme == .light ?
Color(.secondarySystemBackground) :
Color(.systemBackground)
)
}
}
}
.listStyle(InsetGroupedListStyle())
Button(action: {}) {
Label("ADD NEW ITEM", systemImage: "plus")
}
.foregroundColor(.white)
.font(.headline)
.frame(height: 64)
.frame(maxWidth: .infinity)
.background(Color.red)
.cornerRadius(5)
.padding(.horizontal)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With