user14890475
user14890475

Reputation:

How can I change the postion of a view inside GeometryReader to start of top of screen in SwiftUI?

I have a VStack inside a GeometryReader which get bigger than screen, I have numbers from 0 to 40, I want make this VStack re-position in this way that number 1 be visible! like this: 1, 2, 3, . . .

PS: I can use another GeometryReader and reading VStack from background of it, but I want easy way, is there any better way?

    GeometryReader { geometry in

    VStack {
        
        ForEach(0..<40, id: \.self) { index in
            
            Text(index.description).font(Font.body.bold()).padding(.bottom)
            
        }

    }.background(Color.yellow).position(x: geometry.size.width/2, y: geometry.size.height/2)

}

enter image description here

Upvotes: 0

Views: 378

Answers (1)

Asperi
Asperi

Reputation: 257819

You don't need geometry reader in this case, it is enough to use alignment of some outer container, like

var body: some View {
    Color.clear.overlay(
        VStack {
            ForEach(0..<40, id: \.self) { index in
                
                Text(index.description).font(Font.body.bold()).padding(.bottom)
                
            }
        }.background(Color.yellow)
        , alignment: .top)           // << here !!
}

Tested with Xcode 12.1 / iOS 14.1

demo

Upvotes: 1

Related Questions