- Published on
SwiftUI布局优先级
- Authors
- Name
Sets the priority by which a parent layout should apportion space to this child.
Views typically have a default priority of 0 which causes space to be apportioned evenly to all sibling views. Raising a view’s layout priority encourages the higher priority view to shrink later when the group is shrunk and stretch sooner when the group is stretched. 视图的默认优先级通常为 0,这会导致空间被平均分配给所有同级视图。提高视图的布局优先级可促使优先级较高的视图在视图组缩小时更晚缩小,在视图组拉伸时更早拉伸。
HStack {
Text("This is a moderately long string.")
.font(.largeTitle)
.border(Color.gray)
Spacer()
Text("This is a higher priority string.")
.font(.largeTitle)
.layoutPriority(1)
.border(Color.gray)
}
In the example above, the first Text element has the default priority 0 which causes its view to shrink dramatically due to the higher priority of the second Text element, even though all of their other attributes (font, font size and character count) are the same. 在上面的示例中,第一个文本元素的默认优先级为 0,尽管它们的所有其他属性(字体、字号和字符数)都相同,但由于第二个文本元素的优先级较高,导致其视图急剧缩小。

A parent layout offers the child views with the highest layout priority all the space offered to the parent minus the minimum space required for all its lower-priority children. 父布局为具有最高布局优先级的子视图提供了所有提供给父视图的空间,减去所有低优先级子视图所需的最小空间。