在 macOS 应用程序上使用 .searchable 时,是否有任何方法可以控制搜索字段的宽度,因为它被添加到工具栏?

Is there any way to control the width of the search field as it's being added to the toolbar while using .searchable on a macOS app?

我正在使用 SwiftUI 编写 macOS 应用程序代码,我正在向它添加 .searchable,将搜索字段放在工具栏上。 macOS 将它添加到工具栏的末尾,这很好。但是,它太宽了。

代码简单

.searchable(
        text: $searchText,
        placement: .toolbar,
        prompt: "Search..."
)

有什么方法可以控制添加到工具栏的搜索字段的宽度吗?

此外,是否有办法将其设置为始终显示为某些 macOS 应用程序的“最小化”方式?

这是它在我的应用程序中的样子:

这就是其中一个 macOS 应用程序的样子:只是一个放大镜。

搜索字段会自动调整大小,其外观视情况而定。如果您有一个可调整大小的 window 并将其拖动得更小,您最终会得到您所描述的外观。

此外,如果您向工具栏添加更多元素或功能,搜索字段最终将折叠成只有放大镜。

您可以使用它向工具栏添加一个空但非常宽的按钮字段,以将搜索字段“推”到一边:

        .toolbar {
            ToolbarItemGroup {
                Button("") {}
                .frame(width: 400)
                .buttonStyle(.plain)
            }
        }