virenabhyankar
virenabhyankar

Reputation: 251

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!

Upvotes: 2

Views: 872

Answers (1)

Asperi
Asperi

Reputation: 257543

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

Upvotes: 2

Related Questions