Centering A Dialog Under A Status Item In Cocoa

So you have an NSStatusItem, and you want it to pop a dialog for whatever reason, perhaps a settings window.  Where should that dialog be positioned?  If you’re not placing it centered on the main screen, you probably want it under the status item itself. How does one get it there?

The basic answer is surprisingly simple.

If you’re like me, you want your dialog to be positioned directly underneath the status item that spawned it.  This makes it obvious to the user, and it simply looks nice.  Unfortunately, however, Cocoa doesn’t give you an obvious way to get the position of the status item on the menu bar, which makes it a bit more difficult to determine your target position.

Since you probably received the action that spawns your dialog from a menu item, however, you can get around the problem.  Just have a look at the window associated with the current event, and off you go:

- (IBAction)myEvent:(id)sender {
  NSRect frame = [[[NSApp currentEvent] window] frame];
  NSSize origin = [[NSScreen screens][0] visibleFrame].size;

  [myDialogWindow setFrameTopLeftPoint:NSMakePoint(frame.origin.x, origin.height)];

  // ... and whatever else you need to display your window.
}

This code will position the window just beneath the menu bar, right at the left edge of your status item.  There are additional tweaks you’ll want to make, of course, such as ensuring there’s actually enough room to display your entire window, but I’ll leave that as an exercise for the reader.

Enjoy!

S.

About Steve

When it comes to the desktop, Steve is a former Amiga, Windows, and Linux user, and as of six years ago, a die-hard Mac head (who, for once, isn't thinking of changing platforms again any time soon). When it comes to the server, Linux is pretty much the only game he plays. He also enjoys hardware hacking, and shouldn't be allowed near a keyboard after the sun sets (or for that matter, after it rises. Don't say I didn't warn you).
This entry was posted in Cocoa, Mac Hacking. Bookmark the permalink.

11 Responses to Centering A Dialog Under A Status Item In Cocoa

  1. Howdy! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m undoubtedly enjoying your blog and look forward to
    new posts.

  2. I’ve been browsing on-line more than three hours nowadays, yet I by no means discovered any fascinating article like yours.
    It’s beautiful value sufficient for me. In my opinion, if all site owners and bloggers made just right content as you probably did, the net
    will likely be much more useful than ever before.

  3. Sheree says:

    Hi, all is going perfectly here and ofcourse every one is sharing
    data, that’s actually excellent, keep up writing.

  4. Bathing Suits
    Kroot are a solid choice for the points. They
    are 3 pts cheaper per model than firewarriors and start at a
    squad of 10 go up to 20 with options of adding even more models(I think
    you can max out at like 35 models between kroot, hounds,
    and krootoxs) Their utility comesnfrom their
    flexibility; they come with Move through Cover, Infiltrate, and stealth(forests) meaning they have a lot of deployment flexibility and mobility as well.
    I love to outflank with them to grab objectives in my opponents backfield.

    Bathing Suits It is near the beginning of my biking season and
    it was the first time this year that I biked to the beach.
    When I got there I was really tired and hot but
    I went for a long walk anyway. After that I alternated between resting and walking.
    I echo what others have said: No one cares what you
    look like naked and no one will be examining your body for flaws or to perv on you in a single gender onsen. Unless you are a transgender person who has not had surgery, there is
    literally no reason to avoid a single gender onsen. The West is a weird place in which nudity is seen as gross or shameful,
    whereas the rest of the world has learned to see it as something totally normal..
    Bathing Suits

    one piece swimsuits Natasha Singer is a technology reporter in the Business section where she covers health technology,
    education technology and consumer privacy. Currently, she is reporting on how tech
    companies are developing digital health tools and their implications for the healthcare industry, medical practice and consumers.
    She was recently a fellow at the Data Society Research Institute in Manhattan.
    one piece swimsuits

    Bathing Suits Reading intends to begin construction in Q2 2016 for projected opening Q4 2017 at an estimated
    total cost of $27.0 million (AU$35.0 million)Courtenay Central entertainment center expansion (Wellington, New
    Zealand): Reading received local City Council planning approval
    in May 2015 for a supermarket development. Note, the previously announced
    Countdown supermarket development with an estimated cost
    of $11.5 million (NZ$17.0 million) had been delayed by severe damage to car park caused from Wellington earthquake.In an August 2013 Seeking Alpha article on Reading, I discussed a valuation of at least $100MM for just the two above mentioned
    New York City redevelopment projects, Cinema 1,2 3 and Union Square,
    combined. Since 2013, New York City real estate values have continued
    to skyrocket and Reading’s plan to further
    redevelop these parcels creates additional long term value for Reading and its shareholders.
    Bathing Suits

    beach dresses Finally among the REITs, we have EPR Properties (EPR), which offers the exciting combination of an 8% yield
    and projected 5% AFFO growth this year and that even includes the non payment of rent on properties
    from an education tenant. The market doesn’t like EPR because it’s a REIT and it owns a lot of
    (high end) cinemas, but concerns on the latter point are overblown. Box office revenues are generally
    stable and AMC is in reasonable fiscal health.. beach dresses

    one piece swimsuits Eating a lot of food does not count as overeating if your
    body needs a lot of food. Eating more food than you think
    you should eat based on some idea you have does
    not count as overeating either. Overeating can be a big problem but so can not eating enough.
    one piece swimsuits

    bikini swimsuit So, my Ikea cabinets are going in (kitchen reno)
    and we run into some issues with the deco strips/side panels.
    Looong story short, they going to cut down the side panels so that they shorter than they are now (3/4″) and not going with deco strip at all. Installer is recommending I go with 1/2″ to hide the lights which will
    have to be led strip lights now, because they miscalculated and I won pass inspection if I
    have less than 18″ finish height between top of counter and bottom of cabinet.. bikini swimsuit

    Sexy Bikini Swimsuit Ah, well. I in the middle of much too much right now. 3 points submitted 9 days agoI getting lazier and lazier with shows. What a world we live in. It is hard though when there is this weird notion that positive forces (god/ vibes/ hope) can just rid people of ailments and there are people who claim it’s worked for them. Maybe it’s easier to trust these strong worded anecdotal stories than scientist without emotional stories? Maybe people don’t like having an illness that needs professional treatment? Who knows Sexy Bikini Swimsuit.

  5. Pretty component to content. I simply stumbled upon your site and in accession capital to claim
    that I acquire in fact loved account your blog posts.

    Any way I will be subscribing in your augment or even I fulfillment you access consistently fast.

  6. Wonderful website. Lots of helpful information here.
    I’m sending it to some buddies ans additionally sharing in delicious.
    And of course, thank you to your sweat!

  7. Just want to say your article is as astonishing. The clarity to your post is simply great and
    i could assume you’re a professional on this subject.

    Fine together with your permission let me to snatch your feed to stay up to date with coming near near post.
    Thanks a million and please continue the rewarding work.

  8. It has a superb antivirus lab scores too.

  9. Associated part beneath for more particulars of the information.

  10. Jessie says:

    This piece of writing will help the internet people for building up new web site
    or even a weblog from start to end.

  11. Archer says:

    Fabulous, what a weblog it is! This weblog presents useful data to us, keep
    it up.

Comments are closed.