SwiftUI - centre content on a List

Viewed 8354

How can I make this arrow on the centre of the list?

struct ProductsList : View {
var body: some View {

    VStack {
        List {
            Image(systemName: "shift")


        }

    }
}

}

enter image description here

3 Answers

You may just wanna use some Spacers.

struct ProductsList : View {
    var body: some View {
        VStack {
            List {
                HStack {
                   Spacer()
                   Image(systemName: "shift")
                   Spacer()
                } 
            }
        }
    }
}

I would suggest to use ViewModifier:

struct CenterModifier: ViewModifier {
    func body(content: Content) -> some View {
        HStack {
            Spacer()
            content
            Spacer()
        }
    }
} 

so that in your list, if you have more different type of UI elements its more convenient way to do so:

struct ExampleList: View {
    var body: some View {
        List {
            Image(systemName: "shift").modifier(CenterModifier())
            SomeOtherView().modifier(CenterModifier())
        }
    }
}

Try this:

var body: some View {
    List {
        GeometryReader { geometry in
            VStack(alignment: .center) {
                Image(systemName: "shift")
            }.frame(width: geometry.size.width)
        }
    }
}

Related