14

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

Mane Manero
  • 3,086
  • 5
  • 25
  • 47

4 Answers4

26

You may just wanna use some Spacers.

struct ProductList : View {
    var body: some View {
        VStack {
            List {
                HStack {
                   Spacer()
                   Image(systemName: "shift")
                   Spacer()
                } 
            }
        }
    }
}
SMP
  • 1,629
  • 7
  • 15
14

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())
        }
    }
}
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Islom Alimov
  • 454
  • 6
  • 11
10

Try this:

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

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
piebie
  • 2,652
  • 21
  • 30
2

View modifier:

import SwiftUI

private struct CenterHorizontallyModifier: ViewModifier {

    func body(content: Content) -> some View {
        HStack(spacing: 0) {
            Spacer()
            content
            Spacer()
        }
    }
}

extension View {

    func centerHorizontally() -> some View {
        modifier(CenterHorizontallyModifier())
    }
}

Usage:

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

I can't comment yet, but this is an improved version of this answer

DamjanDabo
  • 97
  • 3