🔸parent view

SwiftUIviewhierarchy ⟩ parent view

註:在 UIKit 中通常稱為 superview

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

Last updated

Was this helpful?