Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - centre content on a List

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

like image 266
Mane Manero Avatar asked Jun 20 '19 19:06

Mane Manero


3 Answers

You may just wanna use some Spacers.

struct ProductsList : View {
    var body: some View {
        VStack {
            List {
                HStack {
                   Spacer()
                   Image(systemName: "shift")
                   Spacer()
                } 
            }
        }
    }
}
like image 143
Sada Avatar answered Oct 24 '22 09:10

Sada


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())
        }
    }
}
like image 8
Islom Alimov Avatar answered Oct 24 '22 10:10

Islom Alimov


Try this:

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

like image 7
piebie Avatar answered Oct 24 '22 10:10

piebie