在我的应用程序中,我在NSToolbar中添加了一个toggleSidebar项。
#if targetEnvironment(macCatalyst)
extension SceneDelegate: NSToolbarDelegate {
func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
return [NSToolbarItem.Identifier.toggleSidebar, NSToolbarItem.Identifier.flexibleSpace, AddRestaurantButtonToolbarIdentifier]
}
}
#endif但是,当我将我的应用程序编译到Catalyst时,该按钮被禁用。有人知道我还需要做些什么才能把它挂起来吗?
发布于 2019-11-03 00:41:50
如果您查看.toggleSidebar/NSToolbarToggleSidebarItemIdentifier的文档,您将看到:
侧边栏的标准工具栏项标识符。它向firstResponder发送toggleSidebar:。
将该方法添加到视图控制器将启用工具栏中的按钮:
Swift:
@objc func toggleSidebar(_ sender: Any) {
}Objective-C:
- (void)toggleSidebar:(id)sender {
}当用户点击工具栏中的按钮时,您的实现将需要执行您想要执行的任何操作。
通常,在使用NSSplitViewController的真实macOS应用程序中,此方法由拆分视图控制器自动处理,您不需要添加自己的toggleSidebar:实现。
发布于 2020-02-21 05:38:45
目标需要更改为self,这显示在这个Apple sample中,它是针对打印项目完成的,但可以很容易地更改为切换拆分项目,就像我在注释之后所做的那样。
/** This is an optional delegate function, called when a new item is about to be added to the toolbar.
This is a good spot to set up initial state information for toolbar items, particularly items
that you don't directly control yourself (like with NSToolbarPrintItemIdentifier).
The notification's object is the toolbar, and the "item" key in the userInfo is the toolbar item
being added.
*/
func toolbarWillAddItem(_ notification: Notification) {
let userInfo = notification.userInfo!
if let addedItem = userInfo["item"] as? NSToolbarItem {
let itemIdentifier = addedItem.itemIdentifier
if itemIdentifier == .print {
addedItem.toolTip = NSLocalizedString("print string", comment: "")
addedItem.target = self
}
// added code
else if itemIdentifier == .toggleSidebar {
addedItem.target = self
}
}
}然后通过添加Swift等效项将该动作添加到场景代理中:
- (IBAction)toggleSidebar:(id)sender{
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
[UIView animateWithDuration:0.2 animations:^{
splitViewController.preferredDisplayMode = (splitViewController.preferredDisplayMode != UISplitViewControllerDisplayModePrimaryHidden ? UISplitViewControllerDisplayModePrimaryHidden : UISplitViewControllerDisplayModeAllVisible);
}];
}发布于 2020-12-09 15:08:26
此答案主要是将@malhal的答案转换为最新的Swift版本
在toolbarDefaultItemIdentifiers.
toolbarWillAddItem中,您需要返回
[.toggleSidebar]。您将编写以下代码(就像前面的答案建议的那样): func toolbarWillAddItem(_ notification: Notification) {
let userInfo = notification.userInfo!
if let addedItem = userInfo["item"] as? NSToolbarItem {
let itemIdentifier = addedItem.itemIdentifier
if itemIdentifier == .toggleSidebar {
addedItem.target = self
addedItem.action = #selector(toggleSidebar)
}
}
}toggleSidebar方法。 @objc func toggleSidebar() {
let splitController = self.window?.rootViewController as? MainSplitController
UIView.animate(withDuration: 0.2) {
splitController?.preferredDisplayMode = (splitController?.preferredDisplayMode != .primaryHidden ? .primaryHidden : .allVisible)
}
}一些可能会有帮助的资源:
https://stackoverflow.com/questions/58673045
复制相似问题