Layout¶
Layout views arrange their controls and accept the common modifiers (width, height, padding, background, and the rest) as constructor parameters.
Stacks¶
| View | Description |
|---|---|
| VStack | Arranges children top to bottom with optional spacing and horizontal alignment. |
| HStack | Arranges children leading to trailing with optional spacing and vertical alignment. |
| ZStack | Layers children on top of each other. |
Containers¶
| View | Description |
|---|---|
| ScrollView | Scrollable region, vertical, horizontal or both. |
| List | Scrollable column of rows with native list styling. |
| Section | Header and footer around a group of rows in a List or Form. |
| Form | Data-entry container with a two-column label/control layout on macOS. |
| Group | Groups views without adding layout. |
Spacing¶
| View | Description |
|---|---|
| Spacer | Expands to fill the free space along a stack's axis. |
Grids¶
| View | Description |
|---|---|
| Grid & GridRow | Grid with explicit rows. All cells are created up front. |
| LazyVGrid & LazyHGrid | Grids defined by column or row specs that create cells as they become visible. |
Navigation¶
| View | Description |
|---|---|
| NavigationStack & NavigationLink | Drill-down navigation with a managed view stack. |
| DisclosureGroup | Collapsible section. |
Choosing a container¶
| Content | Use |
|---|---|
| A few views that fit in the popover | VStack / HStack |
| Content taller or wider than the popover | ScrollView around the content |
Rows with system separators and insets, optionally grouped by Section |
List |
| Labeled settings controls | Form, with Section for groups |
| Cells that line up in columns, a fixed number of rows | Grid |
| Many items flowing into columns (or rows) | LazyVGrid (or LazyHGrid) inside a ScrollView |
| Tabular data with headers and selection | Table |
List scrolls on its own. VStack, Grid, LazyVGrid and LazyHGrid do not: wrap them in a ScrollView when the content can exceed the space available.
How sizing works¶
The popover is app.width by app.height points (300 by 400 by default) and the root view is centered in it. Inside it:
- A stack is as large as its children plus
spacing. Extra space in the parent is left around the stack, not inside it. - A
Spacertakes the free space along its stack's axis. Several spacers in one stack split it equally. A spacer has no effect when the stack is only as large as its content. widthandheightfix a view's size.max_width="infinity"(ormax_height) makes the view fill its parent along that axis.paddingis applied before the frame, so it counts towardwidthandheight.backgroundis applied after the frame and covers the whole frame. See Modifiers for the full order.
Alignment¶
A stack's alignment positions its children relative to each other across the stack's axis. It does not move the stack inside its parent: a leading-aligned VStack is still centered in the popover. To push it to an edge, place it in an HStack next to a Spacer:
nib.HStack(
controls=[
nib.VStack(
controls=[
nib.Text("Account", font=nib.Font.HEADLINE),
nib.Text("user@example.com", foreground_color=nib.Color.SECONDARY),
],
alignment=nib.HorizontalAlignment.LEADING,
spacing=4,
),
nib.Spacer(),
],
padding=16,
)
Each container takes a different alignment type. A string value is accepted too; a value outside the accepted set falls back to center.
| View | Enum | Accepted values |
|---|---|---|
VStack |
HorizontalAlignment |
leading, center, trailing |
HStack |
VerticalAlignment |
top, center, bottom |
ZStack |
Alignment |
all nine values |
Grid |
Alignment |
all nine values |
GridRow |
VerticalAlignment |
top, center, bottom |
LazyVGrid |
HorizontalAlignment |
leading, center, trailing |
LazyHGrid |
VerticalAlignment |
top, center, bottom |
GridItem.alignment |
Alignment |
all nine values |
Nesting stacks¶
Most interfaces are stacks inside stacks. A header row over a list of contact rows:
import nib
def contact_row(name, email):
return nib.HStack(
controls=[
nib.SFSymbol("person.circle.fill", foreground_color=nib.Color.BLUE),
nib.VStack(
controls=[
nib.Text(name, font=nib.Font.HEADLINE),
nib.Text(email, font=nib.Font.CAPTION, foreground_color=nib.Color.SECONDARY),
],
alignment=nib.HorizontalAlignment.LEADING,
spacing=2,
),
nib.Spacer(),
],
spacing=10,
)
def main(app: nib.App):
app.title = "Contacts"
app.icon = nib.SFSymbol("person.2")
app.build(
nib.VStack(
controls=[
nib.HStack(
controls=[
nib.Text("Contacts", font=nib.Font.TITLE),
nib.Spacer(),
nib.Button("Add", on_click=lambda: None),
],
),
contact_row("Alice", "alice@example.com"),
contact_row("Bob", "bob@example.com"),
nib.Spacer(),
],
spacing=8,
padding=16,
)
)
nib.run(main)
The trailing Spacer in each row pushes the text to the leading edge; the one at the end of the outer VStack pushes the content to the top of the popover.
Related¶
- Layout Modifiers:
width,height,max_width,padding - Alignment enums
- Divider
- Views and Events