2

I need to align a NSToolbarItem according to my SplitView. I was looking for something similar to this, but I do not think it is a good idea to use apple's private method. Then I came across this answer, that did the trick.

This is what I have now:

func addToolbarConstraints() {
    if let toolbarItems = delegate.baseWindowController.window?.toolbar?.items {
        if let n = toolbarItems.index(where: { $0.label == "EditPreview"}) {

            toolbarItems[n].view!.translatesAutoresizingMaskIntoConstraints = false

            toolbarItems[n].minSize = CGSize(width: 200.0, height: 0.0)
            toolbarItems[n].maxSize = CGSize(width: 1000.0, height: 100.0)
            toolbarItems[n].view!.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

        }
    }
}

Now to the real issue. Unfortunately this will stop working when user enters fullscreen mode. The reason for that is the following error:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'

Indeed, if we check the View UI Hierarchy through debugger, we can note how the NSToolbar will no longer be under the main "NSWindow - Window" view hierarchy. Instead, there will be a separate item called NSToolbarFullScreenWindow:

https://i.stack.imgur.com/tA65L.png

I believe this is the root cause... Any idea to make this works also when users goes into fullscreen mode?

rs16
  • 21
  • 4

0 Answers0