๐ฆ GeometryReader
(struct)
SwiftUI โฉ views โฉ measuring โฉ GeometryReader
โญ๏ธ GeometryReader
๏ผ
ๆ่ฎๅๆฏ่ฆๅ็ไฝ็ฝฎๅฐบๅฏธ่ณ่จใ
ๆๅ็จๆฏ่ฆๅ็ๆๆ็ฉบ้ใ
่ฒ ่ฒฌๅฐ้ฝๅญ่ฆๅๆผๅทฆไธ่งโ๏ธ (โญ๏ธ) ๐ GeometryReader ็ๅฐ้ฝๆนๅผ
็ถ้่ฆ็ฅ้ๆ่ฆๅ็ๅๆ
ๅฐบๅฏธๆ๏ผๅฏๆญ้
.overlay
ๆ .background
ไฝฟ็จ๏ผไพๅฆ๏ผ
// view > overlay > GeometryReader
view.overlay { // overlay.frame = view.frame
// โญ๏ธ GeometryReader ็ฅ้ view ็ๅฐบๅฏธ๏ผ้้ `geo.size`๏ผ
GeometryReader { geo in
// geo.size ๏ผparaent's size
// geo.frame(in: .global)๏ผparent's bounds
}
}
๐ก ่จป๏ผไธ้ข็จๅผ็ขผไธญ๏ผGeometryReader
็ๆฏ่ฆๅๅ
ถๅฏฆๆฏ view.overlay
๏ผไธ้ view.overlay
็ frame
่ view
็ไธๆจฃโ๏ธ
often used to do custom view layout.
used to solve the problem with .readSize().
used by .getSize() to do something with view's size.
Anchor Preferences Using the subscript, you can get the size and position of any subview down the view tree.
็จๆณ๏ผ
GeometryReader { geometry in
// geometry.size๏ผsize of the paraent
// geometry.frame(in: .global)๏ผbounds of the parent
}
A GeometryReader is con๏ฌgured with a ViewBuilder (just like any other container view), but unlike other containers, the view builder for a geometry reader receives a parameter: the GeometryProxy.
The proxy has a property for the viewโs proposed layout size and a subscript to resolve anchors.
GeometryReader reports its proposed size back as the actual size. Because of this sizing behavior, geometry readers are often especially useful when used as the background or overlay of another view: they become the exact size of the view. We can use this size either to draw something in the bounds of the view or to measure the size of the view. ๐ Thinking in SwiftUI, p.89, Ch.5, Custom Layout - GeometryReader
SwiftUI โฉ
CoreGraphics โฉ CGRect
่ชๅทฑๆๅ็๏ผ(ๅ ถๅฏฆๅ ฉๅๅ้กๆฏ้กไผผ็๏ผๅชๆฏ่ชๅทฑๅง็ตๆฒๆๆ๐)
Last updated
Was this helpful?