PythonIDE Docs
中文
简体中文

几何与特殊布局 API

GeometryReader、ViewThatFits、Group、Overlay 和 SafeAreaInset。

GeometryReader / ViewThatFits / Group / Overlay / SafeAreaInset。


#GeometryReader

签名

text
GeometryReader(content=None, on_change=None, onChange=None,
               children=None, on_geometry=None, onGeometry=None)

参数

参数类型说明
content / childrenView \| list[View] \| None填充可用区域的内容。
on_change / onChange / on_geometry可调用 \| None尺寸变化时触发。默认传入单参数"宽度,高度" 形式的字符串(如 "390.0,844.0")。若回调在签名上接受两个必选位置参数,则运行时会拆分为 (width: float, height: float) 调用。

示例

python
import appui

state = appui.State(w=0.0, h=0.0)


def update_geometry(width, height):
    state.batch_update(w=float(width), h=float(height))


def body():
    return appui.GeometryReader(
        content=appui.VStack([
            appui.Text(f"{state.w:.0f} × {state.h:.0f}").font("title3"),
        ]),
        on_change=update_geometry,
    ).padding()

appui.run(body, state=state, presentation="sheet")

参阅ViewThatFitsScrollView


#ViewThatFits

签名

text
ViewThatFits(content=None)

参数

参数类型说明
contentlist[View] \| None按顺序尝试子视图,采用第一个可在当前约束下布局成功的方案。

示例

python
import appui

def body():
    return appui.ViewThatFits([
        appui.HStack([appui.Text("宽屏一行标题")]),
        appui.VStack([appui.Text("窄屏"), appui.Text("两行标题")]),
    ]).padding()

appui.run(body, presentation="sheet")

参阅HStackVStack


#Group

签名

text
Group(content=None)

参数

参数类型说明
contentlist[View] \| None透明容器,不参与自身布局,用于组合或修饰器作用域。

示例

python
import appui

def body():
    return appui.VStack([
        appui.Group([
            appui.Text("A"),
            appui.Text("B"),
        ]),
    ], spacing=4).padding()

appui.run(body, presentation="sheet")

参阅VStackForEach


#Overlay

签名

text
Overlay(content=None, overlay=None, alignment='center')

参数

参数类型说明
contentView \| None承载视图。
overlayView \| None叠放在上的视图。
alignmentstrZStack 相同的对齐关键字。

示例

python
import appui

def body():
    return appui.Overlay(
        content=appui.Image(system_name="bell"),
        overlay=appui.Text("3").font("caption2").padding(4),
        alignment="topTrailing",
    ).padding()

appui.run(body, presentation="sheet")

参阅ZStackBadge


#SafeAreaInset

签名

text
SafeAreaInset(edge='bottom', content=None)

参数

参数类型说明
edgestr嵌入安全区的一侧,如 bottom
contentView \| None持久显示的条带内容。

示例

python
import appui

def body():
    return appui.VStack([
        appui.Text("主内容区域").frame(max_height=appui.infinity),
        appui.SafeAreaInset(
            edge="bottom",
            content=appui.Text("底部工具条").padding(),
        ),
    ])

appui.run(body, presentation="sheet")

参阅VStackScrollView