🔸parent view
SwiftUI ⟩ view ⟩ hierarchy ⟩ parent view
parent view (父視圖或母視圖)
- 本身是 container view,例如: - ParentView- { ChildView }中的- ParentView。
- 被 view modifier 修飾過後的視圖,例如: - childView.modifier()就是- childView的 parent view。
root view 與其所含的所有子視圖 (descendants) 形成 view hierarchy。
在 container view 的狀況,大括號裡面的 views 就是 child views。
// 被 container view 所包含的 view
HStack {             // parent
    Text("Hello")    // child
    Color.red        // child
}在有 view modifiers 的情況下,要倒過來看,前面的是 child view,後面的是 parent view。
// 被 view modifier 修飾的 view
view                                   // child
    .padding()                         // parent
    .frame(width: 100, height: 100)    // grand parent
    .border(.black)                    // grand grand parent如果結合起來看:
// container view + view modifiers
HStack {             // parent
    Text("Hello")    // child
    Color.red        // child
}
.padding()                         // parent
.frame(width: 100, height: 100)    // grand parent
.border(.black)                    // grand grand parent這時的 view hierarchy 就會變成:
// view hierarchy
border ── frame ── padding ── HStack ─┬─ Text
                                      └─ Color.red- container view 一定是 parent view,但 parent view 不一定是 container view。 
Last updated
Was this helpful?