Mobile NavBar 导航栏
移动端页面顶部导航栏,提供返回、居中标题和右侧文字或图标操作。
何时使用
- 页面需要移动端返回入口和标题时使用
NavBar。 - 右侧只有一个明确操作时使用
Right或RightIcon;复杂工具栏应单独布局。 - 不要用 NavBar 代替桌面端
PageHeader或应用级导航。 - 返回事件由
BackClicked读取,实际路由返回仍由页面负责。
在线体验
切换真实 gallery 的返回和右侧操作,观察 NavBar 在手机画布中的布局。
基本用法
链式方法配置两侧内容;Back("") 使用默认返回图标并隐藏返回文字。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
"github.com/tangoui/tango/ui/mobile"
)
func page(c *ui.Context) {
nav := mobile.NavBar(c, "设置").Back("").RightIcon(icon.More).RightLabel("更多")
if nav.BackClicked() {
// 调用应用的返回逻辑
}
if nav.RightClicked() {
ui.Text(c, "打开更多菜单")
}
}常见用法
返回文字
传入非空文本可同时显示返回箭头和文字。
func backText(c *ui.Context) {
mobile.NavBar(c, "详情").Back("返回")
}右侧文字按钮
Right 适合短文本动作,点击状态从同一个 builder 读取。
func rightAction(c *ui.Context) {
nav := mobile.NavBar(c, "编辑").Back("").Right("保存")
if nav.RightClicked() {
ui.Text(c, "已保存")
}
}图标和辅助标签
用 RightIcon 提供图标,用 RightLabel 提供屏幕阅读器或视觉辅助标签。
func iconAction(c *ui.Context) {
mobile.NavBar(c, "消息").RightIcon(icon.Plus).RightLabel("新建消息")
}控制分隔线
Border(false) 可关闭底部分隔线,让导航栏与自定义首屏内容无缝连接。
func borderless(c *ui.Context) {
mobile.NavBar(c, "首页").Border(false)
}主题与样式
NavBar 读取当前 Provider 的移动主题令牌;Ant Mobile、Vant 和 shadcn 会调整高度、边框和文字层级。不要直接写颜色覆盖标题,应用内容可通过 mobile.ThemeOf(c) 读取令牌。
注意事项
NavBar 的 builder 应在每帧调用一次,并在同一帧读取 BackClicked 或 RightClicked。返回文字要保持简短;为图标动作设置 RightLabel,保证屏幕阅读器能说明动作。RTL 下返回箭头和左右槽位会自动镜像,不要手动交换内容。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Mobile · 导航:NavBar / SearchBar / Tabs / NoticeBar / Grid / TabBar
gallery 演示 MobileNav 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
mobile.NavBar
func NavBar(c *ui.Context, title string) *NavBarWNavBar adds a navigation bar titled title.
ConfigProvider defaults ("mobile.NavBar"): "bordered" (bool).
返回 *NavBarW,可链式调用的方法见 NavBarW 方法。
mobile.TabBar
func TabBar(c *ui.Context, items []TabItem, active *string) *TabBarWTabBar adds a bottom tab bar; *active holds the selected item's Key. It reserves the bottom safe-area inset below the items.
ConfigProvider defaults ("mobile.TabBar"): "bordered" (bool, the top line; default on).
返回 *TabBarW,可链式调用的方法见 TabBarW 方法。
NavBarW 方法
mobile.NavBarW:NavBarW is the top navigation bar: back arrow and text at the start (left; right in RTL, the arrow mirrored to point right), centred title, text or icon at the end.
| 方法 | 说明 | 签名 |
|---|---|---|
Back | Back shows the back arrow with optional text (Ant Mobile shows the arrow only by default; Vant styles typically pass "返回"). | Back(text string) *NavBarW |
BackClicked | BackClicked reports a tap on the back area. | BackClicked() bool |
Border | Border toggles the bottom hairline (Vant and shadcn default on, Ant Mobile off). | Border(on bool) *NavBarW |
ID | ID returns the bar's ID. | ID() ui.ID |
Right | Right shows a text action at the end (right; left in RTL). | Right(text string) *NavBarW |
RightClicked | RightClicked reports a tap on the right action. | RightClicked() bool |
RightIcon | RightIcon shows an icon action at the end; name it with RightLabel unless Right gives it a text. | RightIcon(ic *icon.Icon) *NavBarW |
RightLabel | RightLabel sets the accessible name of the right action (needed for an icon-only action). | RightLabel(s string) *NavBarW |
TabBarW 方法
mobile.TabBarW:TabBarW is the bottom tab bar.
| 方法 | 说明 | 签名 |
|---|---|---|
Border | Border toggles the top line (default on). | Border(on bool) *TabBarW |
Changed | Changed reports a tab switch this frame. | Changed() bool |
ID | ID returns the bar's ID. | ID() ui.ID |
TabItem 参数类型
mobile.TabItem:TabItem is one entry of a TabBar.
| 字段 | 说明 | 类型 |
|---|---|---|
Key | string | |
Title | string | |
Icon | *icon.Icon | |
Badge | "" none, "dot" for a dot, else the count text | string |