๐Ÿ”ธchild view

SwiftUI โŸฉ view โŸฉ hierarchy โŸฉ child view

child view (ๅญ่ฆ–ๅœ–)

  • ่ขซ container view ๆ‰€ๅŒ…ๅซ็š„ view๏ผŒไพ‹ๅฆ‚๏ผš ContainerView { childView }ใ€‚

  • ่ขซ view modifier ไฟฎ้ฃพ็š„ view๏ผŒไพ‹ๅฆ‚๏ผšchildView.modifier() ไธญ็š„ childViewใ€‚

root view ่ˆ‡ๅ…ถๆ‰€ๅซ็š„ๆ‰€ๆœ‰ๅญ่ฆ–ๅœ– (descendants) ๅฝขๆˆ view hierarchyใ€‚

่จป๏ผšๅœจ UIKit ไธญ้€šๅธธ็จฑ็‚บ subviewใ€‚

ๅœจ 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