You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在macOS的Toolbar中将ToolbarItem放置在尾部边缘

macOS Toolbar尾部放置ToolbarItem解决方案

方案1:使用.secondaryAction位置属性(推荐)

你查阅的属性列表遗漏了macOS专属的枚举值,.secondaryAction的默认行为就是将元素放在Toolbar的最右侧尾部边缘,是系统推荐的标准实现,支持macOS 13及以上版本,会自动适配窗口控制按钮、标题区域的间距,不会出现布局冲突。
示例代码:

Toolbar {
    ToolbarItem(placement: .secondaryAction) {
        // 替换为你自己的控件
        Button(action: {}) {
            Image(systemName: "gearshape")
        }
    }
}

方案2:自定义布局适配低版本

如果需要兼容macOS 13以下的版本,可以通过占满全宽的HStack配合Spacer()将目标元素强制顶到尾部:
示例代码:

Toolbar {
    ToolbarItem(placement: .principal) {
        HStack {
            // 左侧区域原有控件:标题、左侧操作按钮等
            Text("窗口标题")
            Button(action: {}) {
                Image(systemName: "sidebar.left")
            }
            
            Spacer() // 撑满中间空白区域,将右侧控件推到尾部
            
            // 你需要放在尾部的控件
            Button(action: {}) {
                Image(systemName: "gearshape")
            }
        }
        .frame(maxWidth: .infinity)
    }
}

补充说明

如果同时存在左侧、中间、右侧多组控件,可以组合使用不同placement:

  • 左侧控件使用.primaryAction
  • 中间控件使用.principal
  • 右侧控件使用.secondaryAction
    系统会自动按区域排列,不会出现元素挤压重叠的问题。

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 22:06:01