gpt4 book ai didi

swift - 如何在 Catalyst 应用程序中将搜索栏添加到 NSToolbar 中?

转载 作者:行者123 更新时间:2023-11-28 05:37:18 25 4
gpt4 key购买 nike

我看到一些 Catalyst 应用程序向 NSToolbar 添加了一个搜索栏,我想知道我如何才能做到这一点。我是否必须导入 AppKit/Cocoa 才能获得 NSSearchField 和实际的 NSToolbar?还是有一些我只是没有看到的 UISearchBars 方法?任何帮助都会很棒,谢谢。

我尝试过导入 AppKit 和 Cocoa(使用捆绑加载器),但我认为我做得不对。如果有人有关于如何执行此操作的可靠指南,请链接它。

我还尝试使用 UISearchBar 的自定义 View 创建一个 UIBarButtonItem,它最终不会呈现任何内容。 (可以在下面找到)

这是在 itemIdentifier.rawValue 的 switch case 中找到的(在工具栏 itemForIdentifier 函数中)

let search = UISearchBar()
search.sizeToFit()
let button = UIBarButtonItem(customView: search)
button.width = 100
let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier, barButtonItem: button)
toolbarItem.isBordered = true
toolbarItem.isEnabled = true
return toolbarItem

最佳答案

感谢 mmackh,可以像添加任何其他工具栏项一样轻松添加搜索栏。感谢所有调查此事的人。

https://github.com/mmackh/Catalyst-Helpers


Swift 示例

一旦您将文件添加到您的项目(在本例中是您的桥接 header ),只需像返回任何其他工具栏项一样返回它。

let item = NSToolbarItem_Catalyst.searchItem(withItemIdentifier: "searchBar", textDidChangeHandler: { string in
print(string)
})

关于swift - 如何在 Catalyst 应用程序中将搜索栏添加到 NSToolbar 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58260748/

25 4 0