VStack, HStack & ZStack

Stacks arrange their controls in the order of the list. VStack places them top to bottom, HStack places them leading to trailing (left to right in left-to-right locales), and ZStack layers them on top of each other. Most interfaces are stacks nested inside stacks; see Nesting stacks for a worked example and How sizing works for how a stack's size is determined.

Shared parameters

Parameter Type Default Description
controls list[View] None Child views, in order.
spacing float None Distance in points between adjacent children. When None, the system default spacing is used. VStack and HStack only.
alignment per stack None How children line up across the stack's axis. Each stack takes a different enum, listed in its section below. Defaults to center.
**modifiers Common view modifiers: padding, background, foreground_color, opacity, width, height, on_drop, on_hover, on_click, and the rest. See Modifiers.

alignment positions children relative to each other, not the stack inside its parent. See Alignment for how to move a stack to an edge and which string values each stack accepts.

Mutable properties

Property Type Description
controls list[View] Get or set the child views. Setting triggers a UI update.

VStack

VStack

nib.VStack(controls=None, spacing=None, alignment=None, **modifiers)

alignment takes a nib.HorizontalAlignment: LEADING, CENTER or TRAILING.

Basic vertical layout

vstack_basic_vertical_layout

import nib


def main(app: nib.App):
    app.title = "Weather"
    app.icon = nib.SFSymbol("cloud.sun")

    app.build(
        nib.VStack(
            controls=[
                nib.SFSymbol("cloud.sun.fill", font=nib.Font.system(40)),
                nib.Text("21°", font=nib.Font.LARGE_TITLE),
                nib.Text("Partly cloudy", foreground_color=nib.Color.SECONDARY),
                nib.Text("H: 24°  L: 15°", font=nib.Font.CAPTION),
            ],
            spacing=6,
            padding=20,
            width=240,
        )
    )


nib.run(main)

Left-aligned stack with styling

vstack_left_aligned_stack

import nib


def main(app: nib.App):
    app.title = "Inbox"
    app.icon = nib.SFSymbol("envelope")
    app.width = 340
    app.height = 200

    app.build(
        nib.VStack(
            controls=[
                nib.Text("Release 1.2 is ready", font=nib.Font.HEADLINE),
                nib.Text("From Sam Lee, 09:41", foreground_color=nib.Color.SECONDARY),
                nib.Text("The build passed every check. Shall we tag it today?"),
                nib.Button(
                    "Reply", on_click=lambda: None, style=nib.ButtonStyle.BORDERED
                ),
            ],
            alignment=nib.HorizontalAlignment.LEADING,
            spacing=8,
            padding=16,
            width=332,
            background=nib.Rectangle(corner_radius=10, fill="#2c2c2e"),
        )
    )


nib.run(main)

HStack

HStack

nib.HStack(controls=None, spacing=None, alignment=None, **modifiers)

alignment takes a nib.VerticalAlignment: TOP, CENTER or BOTTOM.

Icon with label

hstack_icon_label

import nib


def main(app: nib.App):
    app.title = "Now playing"
    app.icon = nib.SFSymbol("music.note")

    app.build(
        nib.HStack(
            controls=[
                nib.SFSymbol("music.note.list", font=nib.Font.system(28)),
                nib.VStack(
                    controls=[
                        nib.Text("Blue in Green", font=nib.Font.HEADLINE),
                        nib.Text("Miles Davis", foreground_color=nib.Color.SECONDARY),
                    ],
                    spacing=2,
                    alignment=nib.HorizontalAlignment.LEADING,
                ),
            ],
            spacing=12,
            alignment=nib.VerticalAlignment.CENTER,
            padding=16,
        )
    )


nib.run(main)

Toolbar with spacers

Use Spacer to push elements to opposite edges. Multiple spacers divide the available space equally.

hstack_toolbar_spacers

import nib


def main(app: nib.App):
    app.title = "Editor"
    app.icon = nib.SFSymbol("square.and.pencil")
    app.width = 360

    app.build(
        nib.VStack(
            controls=[
                nib.HStack(
                    controls=[
                        nib.Button("Back", icon="chevron.left", on_click=lambda: None),
                        nib.Spacer(),
                        nib.Text("Untitled", font=nib.Font.HEADLINE),
                        nib.Spacer(),
                        nib.Button(
                            "Done",
                            on_click=lambda: None,
                            style=nib.ButtonStyle.BORDERED_PROMINENT,
                        ),
                    ],
                    spacing=8,
                ),
                nib.Divider(),
                nib.Text(
                    "Spacers push the buttons to the edges and centre the title.",
                    foreground_color=nib.Color.SECONDARY,
                ),
            ],
            spacing=10,
            padding=16,
            width=352,
        )
    )


nib.run(main)

ZStack

ZStack

nib.ZStack(controls=None, alignment=None, **modifiers)

The first child in controls is at the back and each following child is drawn on top of the previous one. Children occupy the same space, so ZStack places one view behind or over another, such as a card background or a badge. It has no spacing parameter.

alignment takes a nib.Alignment: CENTER, TOP, BOTTOM, LEADING, TRAILING, TOP_LEADING, TOP_TRAILING, BOTTOM_LEADING or BOTTOM_TRAILING.

Card with background

zstack_card_background

import nib


def main(app: nib.App):
    app.title = "Card"
    app.icon = nib.SFSymbol("creditcard")

    app.build(
        nib.ZStack(
            controls=[
                nib.Rectangle(
                    corner_radius=14,
                    fill=nib.LinearGradient(
                        colors=["#5e5ce6", "#bf5af2"], start=(0, 0), end=(1, 1)
                    ),
                ),
                nib.VStack(
                    controls=[
                        nib.Text(
                            "nib bank",
                            font=nib.Font.CAPTION,
                            foreground_color=nib.Color.WHITE.with_opacity(0.8),
                        ),
                        nib.Spacer(),
                        nib.Text(
                            "•••• 4242",
                            font=nib.Font.TITLE3,
                            foreground_color=nib.Color.WHITE,
                        ),
                        nib.Text(
                            "JANE DOE",
                            font=nib.Font.CAPTION,
                            foreground_color=nib.Color.WHITE,
                        ),
                    ],
                    alignment=nib.HorizontalAlignment.LEADING,
                    padding=16,
                ),
            ],
            width=300,
            height=190,
            padding=20,
        )
    )


nib.run(main)

Badge overlay

Position a notification badge in the top-trailing corner of an icon.

zstack_badge_overlay

import nib


def main(app: nib.App):
    app.title = "Alerts"
    app.icon = nib.SFSymbol("bell")

    def badge(icon, count):
        return nib.ZStack(
            controls=[
                nib.SFSymbol(icon, font=nib.Font.system(30)),
                nib.Text(
                    str(count),
                    font=nib.Font.CAPTION2,
                    foreground_color=nib.Color.WHITE,
                    padding={"horizontal": 5, "vertical": 1},
                    background=nib.Capsule(fill=nib.Color.RED),
                ),
            ],
            alignment=nib.Alignment.TOP_TRAILING,
        )

    app.build(
        nib.HStack(
            controls=[
                badge("bell.fill", 3),
                badge("envelope.fill", 12),
                badge("bubble.left.fill", 1),
            ],
            spacing=28,
            padding=20,
        )
    )


nib.run(main)