几何与特殊布局 API
GeometryReader、ViewThatFits、Group、Overlay 和 SafeAreaInset。
GeometryReader / ViewThatFits / Group / Overlay / SafeAreaInset。
#GeometryReader
签名
已复制
GeometryReader(content=None, on_change=None, onChange=None,
children=None, on_geometry=None, onGeometry=None)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
content / children | View \| list[View] \| None | 填充可用区域的内容。 |
on_change / onChange / on_geometry | 可调用 \| None | 尺寸变化时触发。默认传入单参数:"宽度,高度" 形式的字符串(如 "390.0,844.0")。若回调在签名上接受两个必选位置参数,则运行时会拆分为 (width: float, height: float) 调用。 |
示例
已复制
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")
#ViewThatFits
签名
已复制
ViewThatFits(content=None)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
content | list[View] \| None | 按顺序尝试子视图,采用第一个可在当前约束下布局成功的方案。 |
示例
已复制
import appui
def body():
return appui.ViewThatFits([
appui.HStack([appui.Text("宽屏一行标题")]),
appui.VStack([appui.Text("窄屏"), appui.Text("两行标题")]),
]).padding()
appui.run(body, presentation="sheet")
#Group
签名
已复制
Group(content=None)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
content | list[View] \| None | 透明容器,不参与自身布局,用于组合或修饰器作用域。 |
示例
已复制
import appui
def body():
return appui.VStack([
appui.Group([
appui.Text("A"),
appui.Text("B"),
]),
], spacing=4).padding()
appui.run(body, presentation="sheet")
#Overlay
签名
已复制
Overlay(content=None, overlay=None, alignment='center')
参数
| 参数 | 类型 | 说明 |
|---|---|---|
content | View \| None | 承载视图。 |
overlay | View \| None | 叠放在上的视图。 |
alignment | str | 与 ZStack 相同的对齐关键字。 |
示例
已复制
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")
#SafeAreaInset
签名
已复制
SafeAreaInset(edge='bottom', content=None)
参数
| 参数 | 类型 | 说明 |
|---|---|---|
edge | str | 嵌入安全区的一侧,如 bottom。 |
content | View \| None | 持久显示的条带内容。 |
示例
已复制
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")
参阅:VStack、ScrollView