macOS Big Sur toolbar item width with image

295 Views Asked by At

I'm trying to create an NSToolbar with items similar to the Apple's Mail app on macOS. I have an issue with the default toolbar item's width though, as it seems to be inconsistent. Since Big Sur, the items are meant to be sized automatically by AppKit and the NSToolbarItem minSize, maxSize properties have been deprecated.

I'm setting the image property for each NSToolbarItem, not using custom views. As you can see in the screenshots below, the envelope icon has a different "highlight" area (less padding on the sides) while the trash icon has a much larger highlight area.

Single item

Group items

The envelope icon is a single NSToolbarItem while the archive box and trash items are displayed using NSToolbarItemGroup with NSSegmentedControl view.

In the Apple's Mail app, even single toolbar items have the same width as the grouped items:

Apple Mail toolbar

How to increase the toolbar item's width when using an image instead of custom view?

1

There are 1 best solutions below

0
Ely On

Deprecating a property and leaving you in the dark how to achieve a until recently simple effect without using the deprecated property is typical for how Apple deals with AppKit nowadays.

I would not be surprised of the Mail app still uses the deprecated minSize property, or that the NSToolbarItem objects are based on NSButton views with a minimum width NSLayoutConstraint (which is my current solution).

To continue using minSize without deprecation warnings, you can consider to use a simple ToolbarItem class like this:

class ToolbarItem: NSToolbarItem {
    override var minSize: NSSize {
        get {
            return NSSize(width: 50, height: 30)
        }
        set {}
    }
}