๐Ÿ”ธ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ใ€‚

่จป๏ผšๅœจ 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