2

在我的应用程序中,我toggleSidebarNSToolbar.

#if targetEnvironment(macCatalyst)
extension SceneDelegate: NSToolbarDelegate {
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return [NSToolbarItem.Identifier.toggleSidebar, NSToolbarItem.Identifier.flexibleSpace, AddRestaurantButtonToolbarIdentifier]
    }
}
#endif

但是,当我将我的应用程序编译到 Catalyst 时,该按钮被禁用。有人知道我还需要做些什么来连接它吗?

4

4 回答 4

1

目标需要更改为self,这在这个Apple 示例中显示,它是为打印项目完成的,但可以像我在评论后所做的那样轻松更改为切换拆分项目。

/** 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-02-20T21:38:45.407 回答
1

如果您查看.toggleSidebar/的文档,NSToolbarToggleSidebarItemIdentifier您将看到:

侧边栏的标准工具栏项标识符。它将 toggleSidebar: 发送到 firstResponder。

将该方法添加到您的视图控制器将启用工具栏中的按钮:

迅速:

@objc func toggleSidebar(_ sender: Any) {
}

目标-C:

- (void)toggleSidebar:(id)sender {
}

当用户点击工具栏中的按钮时,您的实现将需要做任何您想做的事情。

通常,在NSSplitViewController使用toggleSidebar:.

于 2019-11-02T16:41:49.590 回答
0

这个答案主要是将@malhal的答案转换为最新的Swift版本

  1. 您将需要返回[.toggleSidebar]toolbarDefaultItemIdentifiers
  2. toolbarWillAddItem您将编写以下内容(就像之前建议的答案一样):
  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)
      }
    }
  }
  1. 最后,您将添加您的toggleSidebar方法。
  @objc func toggleSidebar() {
    let splitController = self.window?.rootViewController as? MainSplitController
    UIView.animate(withDuration: 0.2) {
      splitController?.preferredDisplayMode = (splitController?.preferredDisplayMode != .primaryHidden ? .primaryHidden : .allVisible)
    }
  }

一些可能有帮助的资源:

  1. 将工具栏和触控栏集成到您的应用中
  2. Mac Catalyst:添加工具栏
于 2020-12-09T07:08:25.920 回答
0

配置时UISplitViewControllerprimaryBackgroundStyle.sidebar

let splitVC: UISplitViewController = //your application's split view controller
splitVC.primaryBackgroundStyle = .sidebar

这将使您能够NSToolbarItem使用系统标识符.toggleSidebar,并且它将自动与UISplitViewControllerMac Catalyst 一起使用,而无需设置任何目标/操作代码。

于 2021-04-03T16:23:32.360 回答