Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

contentInset on SwiftUI's List

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.

enter image description here

My current solution is the following

Section(header: Color.clear.frame(height: 64)) { EmptyView() }

but I wonder is there a better way?

like image 853
LukasHromadnik Avatar asked Sep 14 '20 15:09

LukasHromadnik


1 Answers

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

enter image description here

like image 77
egeeke Avatar answered Sep 20 '22 11:09

egeeke