GeometryReader enables automatically adapting to different screen sizes
Use GeometryReader's relative coordinates
can hold multiple views
expands to take up as much space as possible
can retrieve its width and height
GeometryReader{ geometry in
VStack{
Text("Width = \(geometry.size.width)")
Text("Height = \(geometry.size.height)")
}
}.background(.yellow)
on iPod touch
on iPad 9th
Coordinates within the GeometryReader are known as local coordinates.
Global coordinates refer to the entire iOS screen.
Global coordinates always differ between different iOS device screens, local coordinates with a GeometryReader always remains consistent.
VStack{
Text("This Text view pushed the GeometryReader down")
HStack{
Text("Pushed to the right")
GeometryReader{ geometry in
VStack{
Text("Local x origin = \(geometry.frame(in: .local).origin.x)")
Text("Local y origin = \(geometry.frame(in: .local).origin.y)")
Text("Global x origin = \(geometry.frame(in: .global).origin.y)")
Text("Global y origin = \(geometry.frame(in: .global).origin.y)")
}
}.background(.yellow)
.ignoresSafeArea()
}
}
top-left of a device's screen is minX and minY
GeometryReader{ geometry in
VStack{
Text("minX = \(geometry.frame(in: .local).minX)")
Text("midX = \(geometry.frame(in: .local).midX)")
Text("maxX = \(geometry.frame(in: .local).maxX)")
Divider()
Text("minY = \(geometry.frame(in: .local).minY)")
Text("midY = \(geometry.frame(in: .local).midY)")
Text("maxY = \(geometry.frame(in: .local).maxY)")
}
}.background(.yellow)
on iPod touch
on iPad 9th