Size constraints for custom view controller for NSToolbarItem

50 Views Asked by At

I have a custom view in a toolbar. Sometime this year Apple deprecated NSToolbarItem.minsize & maxsize, and so now I get compiler warnings. How to replace the deprecated constraints in the current version of macOS for a custom view (drawn during runtime)? I have been searching and searching and I cannot figure it out.

1

There are 1 best solutions below

0
Willeke On

From AppKit Release Notes for macOS 10.14:

Automatically Sized Instances of NSToolbarItem

Beginning in apps linked on the macOS 10.14 SDK, if an NSToolbarItem doesn’t have its minSize and maxSize properties set, these values are calculated automatically by AppKit using constraints, similar to how the size properties for NSTouchBarItem are set. (Previously, if the item’s minimum and maximum size properties weren’t set, the size of the containing view was used.) You can use constraints to define both a minimum and maximum size that will be calculated by AppKit. For example, you can create the following constraints to give the view a minimum width of 100 and a maximum width of 200:

view.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true view.widthAnchor.constraint(lessThanOrEqualToConstant: 200).isActive = true

The same applies for height. Omitting a minimum width or height constraint will use the view’s intrinsic content size, which is automatically adjusted for localization purposes for controls.

If the view being measured has an undefined height or width, the view’s frame size is used instead.