简体   繁体   中英

macOS Big Sur toolbar item width with image

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.

单品

组项目

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 邮件工具栏

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

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 {}
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM