如何将 ToolbarItem 放置在菜单栏的右侧?

Tho*_*mas 5 macos swiftui

SwiftUI、macOS。

当我尝试这样做时:

ToolbarItem (placement: .navigationBarTrailing) {
                            // content
                        }
Run Code Online (Sandbox Code Playgroud)

我收到编译错误:“navigationBarTrailing”在 macOS 中不可用

那么如何将内容放置在菜单栏的右侧呢?

Asp*_*eri 0

我假设您指的是工具栏(因为 macOS 应用程序主菜单栏是唯一的,不允许将项目放在右侧 - 您需要NSStatusItem为此进行操作)。

因此,对于工具栏,我相信您可以使用confirmationAction放置将项目强制放置在尾随(右侧)位置。请参阅下面的苹果文档:

    /// The item represents a confirmation action for a modal interface.
    ///
    /// ...
    ///
    /// In macOS and in Mac Catalyst apps, the system places
    /// `confirmationAction` items on the trailing edge
    /// in the trailing-most position of the sheet and gain the apps accent
    /// color as a background color.
    ///
    /// ...
    /// 
    public static let confirmationAction: ToolbarItemPlacement
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。是的,它有效,但话又说回来,也不完全有效。我还在中心放置了一个项目(位置:.principal)。出于某种原因,我似乎无法两者兼得。有任何想法吗? (2认同)