.dimension()
Last updated
Last updated
SwiftUI โฉ Views โฉ View โฉ .dimension() โฉ
ไฝฟ็จ่ช่ฃฝๅทฅๅ
ท view.dimension()
ไพๆจ็คบไธๅ่ฆๅ็้ทๅฏฌใ
้ทๅฏฌๆจ็คบ็ไฝ็ฝฎๅฏ็จ DimensionPositions ไพๆๅฎใ
struct PlayView: View {
// view body
var body: some View {
HStack(spacing: 50) {
colorBlock(.pink)
// โญ๏ธ ๆๅฎๆจ็คบไฝ็ฝฎ็บใๅๆนใ่ใๅ็ดไธญๅคฎใ
.dimension([.leading, .vcenter])
colorBlock(pacificBlue)
// โญ๏ธ ๆน่ฎๆธๅญ็้ก่ฒ
.dimension([.hcenter, .bottom], label: .yellow)
colorBlock(.purple)
// โญ๏ธ ๆน่ฎ็ฎญ้ ญ็้ก่ฒ
.dimension(.bottomTrailing, arrow: .blue)
}
}
/// color block
func colorBlock(_ color: Color) -> some View {
color
.frame(width: 200, height: 100)
.border(Color.primary)
}
}
โฌ๏ธ ้่ฆ๏ผ DimensionPositions, WidthDimension, HeightDimension
extension View {
func dimension(
// โญโโ๐ OptionSetโโโฎ
_ positions: DimensionPositions = [.bottom], // โญ๏ธ ๆจ็คบ็ไฝ็ฝฎ
arrow: Color = .secondary, // โญ๏ธ ็ฎญ้ ญ้ก่ฒ
label: Color = .primary // โญ๏ธ ๆธๅญ้ก่ฒ
) -> some View
{
overlay(alignment: .top) {
// ๐ DimensionPositions
if positions.contains(.top) {
// ๐ WidthDimension
WidthDimension(label: label, arrow: arrow).offset(y: -25)
}
}
.overlay(alignment: .bottom){
if positions.contains(.bottom){
WidthDimension(label: label, arrow: arrow).offset(y: 25)
}
}
.overlay {
if positions.contains(.vcenter){
WidthDimension(label: label, arrow: arrow)
}
}
.overlay(alignment: .leading) {
if positions.contains(.leading) {
// ๐ HeightDimension
HeightDimension(label: label, arrow: arrow).offset(x: -25)
}
}
.overlay(alignment: .trailing) {
if positions.contains(.trailing) {
HeightDimension(label: label, arrow: arrow).offset(x: 25)
}
}
.overlay {
if positions.contains(.hcenter) {
HeightDimension(label: label, arrow: arrow)
}
}
}
}
uses DimensionPositions to put width/height dimensions in position.
uses WidthDimension to draw a width dimension.
uses HeightDimension to draw a height dimension.
used by grid layout algorithm to show width dimension.
TestFrameView - show view's frame behaviors.