1.3.4 工具栏(Toolbars)



SwiftUI 将 navigationBar、bottomBar、tabBar、windowToolbar 等视图的功能和样式都整合在了工具栏中,使用时调用的 api 都是以 .toolbar 开头的。

比如要给导航栏右侧添加一个分享按钮:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#if os(iOS)
NavigationStack {
  ContentView()
  .toolbar {
    ToolbarItem(placement: .navigationBarTrailing) {
      Button {
      } label: {
        Image(systemName: "square.and.arrow.up")
      }
    }
  }
}
#endif

> 查看示例

placement 参数是 ToolbarItemPlacement 类型,表示 item 放置的位置。如果需要在同一位置放置多个 item,可以使用 ToolbarItemGroup :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
ToolbarItemGroup(placement: groupPlacement) {
  Button {
  } label: {
    Image(systemName: "square.and.arrow.up")
  }
  Menu {
    Button("Menu 1") {}
    Button("Menu 2") {}
  } label: {
    Image(systemName: "pencil")
  }
}

var groupPlacement: ToolbarItemPlacement {
  #if os(iOS)
  .navigationBarTrailing
  #else
  .primaryAction
  #endif
}

如果需要改变 toolbar 的背景,可以这样设置:

1
2
3
4
5
6
7
8
9
#if os(iOS)
.toolbarBackground(.yellow, for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)
#elseif os(macOS)
.toolbarBackground(.linearGradient(
  colors: [.blue, .purple],
  startPoint: .leading,
  endPoint: .trailing), for: .windowToolbar)
#endif

这里的 for 参数是 ToolbarPlacement 类型,表示 toolbar 的位置。

toolbar 还支持以下特性:

以上在示例中都有演示,都是比较简单的操作,就不再展开来讲了。

更多关于工具栏的特性,可以探索:Toolbars。

不过 Toolbars 除灵活性不足之外,还有不少明显的问题,比如 .toolbarTitleMenu 只在 iOS 和 iPadOS 上有效,设置 .toolbarBackground 时需要同时指定 .toolbarBackground(.visible, for: ToolbarPlacement...) 才仅在部分 ToolbarPlacement 生效。诸如此类,亟待完善。