Is it possible to add a NSStatusItem to a specific position in NSStatusBar?

↘锁芯ラ 提交于 2019-12-01 03:19:01

问题


I use self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; to add an statusItem to the systemStatusBar on OSX. The statusItem then appears on the left-most position in the systemStatusBar. I was wondering if there is a way to add such an item to a specific index e.g. on the left side of the system clock?


回答1:


Yes and no. Using private API you can specify priority for adding NSStatusItem. I have developed a tiny category for NSStatusBar (NSStatusBar+MISSINGOrder) witch provides simple method for this:

NSStatusItem *statusItem =
    [systemStatusBar statusItemWithLength:NSVariableStatusItemLength
                               positioned:NSStatusBarItemOrderingModeAfter
                               relativeTo:NSStatusBarItemPriorityNotificationCenter];

You can look inside for implementation details.




回答2:


There is a private API to do this:

NSStatusItem *item = [NSStatusBar _statusItemWithLength:NSSquareStatusItemLength  withPriority:INT32_MAX];

That will place the status item all the way to the right (or at least, as far to the right as you're allowed to have them. It's still to the left of spotlight).

Obviously being a private API you should wrap it in a responseToSelector if statement.




回答3:


No. Apple does not provide an API to choose the position.



来源:https://stackoverflow.com/questions/19975536/is-it-possible-to-add-a-nsstatusitem-to-a-specific-position-in-nsstatusbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!