Modifiers (修飾符)
- 用來改變 View 的外觀或行為
- 如邊距、尺寸、背景顏色等
padding (內邊距)
- 設定內邊距
edges
- 傳入
Edge.Set 設定內邊距範圍 (default .all)
length
VStack() {
Text("A")
Text("B")
Text("C")
}.padding()
frame (限制尺寸)
- 設定尺寸
- 預設會依據元件內容自動計算尺寸
min
ideal
max
alignment
- 元件在框架中的對齊方式 (通常元件大小等同於框架)
fixedSize (根據內容計算尺寸)
aspectRatio (長寬比例)
layoutPriority (空間分配優先度)
offset (位移)
Text("Hello")
.offset(x: 100)
position (座標)
- 基於父容器的絕對座標做定位 (會影響到父容器的佈局)
Text("Hello")
.position(x: 50, y: 100)
alignmentGuide (對其指南)
HStack(alignment: .firstTextBaseline, spacing: 10) {
Text("HStack")
Text("HStack")
.alignmentGuide(.firstTextBaseline) { dimension in
dimension[.bottom]
}
}
foregroundColor (前景色)
background (背景)
overlay (疊加)
- overlay 為 View 添加額外內容
- 預設會繼承 View 的尺寸
- 如邊框、標籤
Text("Hello")
.padding()
.background(Color.blue)
.overlay(
Circle()
.fill(Color.red)
.frame(width: 10, height: 10),
alignment: .topTrailing
)
opacity (透明度)
shadow (陰影)
clipShape (剪裁)