iOS 26|使用新的 Toolbar 子组件及修饰器

了解如何使用iOS 26 上新推出的 ToolbarSpacer 组件,来个性化定制你的工具栏外观。

ToolbarSpacer()

在 iOS 26 中,Toolbar 中的 ToolbarItem 默认会被放在一个 Group 中显示,即使 未使用 ToolbarItemGroup:

SwiftUI 提供了一个专用的 ToolbarSpacer() 视图,用于将他们分开显示。

为了兼容老版本系统,可以这样使用:

if #available(iOS 26.0, *) {
    ToolbarSpacer(.fixed)
}

sharedBackgroundVisibility

在 iOS 26上,Toolbar 会默认为每个 ToolbarItem 添加背景。

你可以使用 .sharedBackgroundVisibility(.hidden) 修饰器来移除背景。

ToolbarItem(placement: .cancellationAction) {
    Text("\(displayCount)")
}
.sharedBackgroundVisibility(.hidden)