问题
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