简体   繁体   中英

How to create the NSMenuItem view for a menu item with an embedded NSProgressIndicator?

I would like to create a menu item with an embedded NSProgressIndicator , similar to the "Wi-Fi: Looking for Networks…" menu item of the Wi-Fi status menu:

Mac上打开的Wi-Fi状态菜单的屏幕截图显示“Wi-Fi:正在寻找网络...”菜单项

I think that I will need to use the setView: method as discussed at: Views in Menu Items . However, when I call setView: and pass an NSProgressIndicator , only the NSProgressIndicator is displayed.

How can the custom menu item view be created to get a similar result as the "Wi-Fi: Looking for Networks…" menu item of the Wi-Fi status menu?

As noted in the document to which you linked:

A menu item with a view does not draw its title, state, font, or other standard drawing attributes, and assigns drawing responsibility entirely to the view.

So, if you want your menu item to look like a normal menu item plus some other stuff, your view has to draw the normal menu item features in addition to the other stuff. So, you could set the menu item's view to a custom view. The progress indicator would be one subview of that custom view, but you would need other subviews for the other features. For example, a text field for the text of the menu item and an image view for a state indicator (if your item shows state).

You'll have to draw highlighting as the item is selected or tracked. You'll also have to do mouse tracking. Apple has some sample code which demonstrates various parts of this:

MenuItemView
CustomMenus
GridMenu

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