Search code examples
uitableviewswiftuialignmentswift5xcode11

SwiftUI: Custom table view cell with content aligned to the center


I'm new to SwiftUI and was wondering how to center the content (horizontally and vertically) inside my table view cells. My current code is as follows:

var body: some View {
    List(list) { item in
        VStack(alignment: .center) {
            Image(systemName: item.imageName)
            Text(item.text)
        }
    }
}

My goal is to align the VStack in the center of the cell. Thanks!


Solution

  • Here is possible approach (alignment: .center is by default, so can be dropped)

    List(list) { item in
        HStack {
          Spacer()
          VStack {
            Image(systemName: item.imageName)
            Text(item.text)
          }
          Spacer()
        }
    }