Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have popovers in Mac Cocoa apps?

Tags:

cocoa

Popovers are heavily used in iPad apps and I really like them. Now I think about how this could be implemented in AppKit on the mac because I have a use case for it.

Do I need a NSWindow subclass to accomplish the overlay or could I also use a normal view?

like image 271
brutella Avatar asked Jan 05 '11 21:01

brutella


People also ask

How do I enable unverified apps on my Mac?

Control-click the app icon, then choose Open from the shortcut menu. Click Open. The app is saved as an exception to your security settings, and you can open it in the future by double-clicking it just as you can any registered app.

How do I download from an unidentified developer Mac?

Open System Preferences. Go to the Security & Privacy tab. Click on the lock and enter your password so you can make changes. Change the setting for 'Allow apps downloaded from' to 'App Store and identified developers' from just App Store.

How do I allow downloads on my Mac?

View the app security settings on your MacIn System Preferences, click Security & Privacy and then click General. Click the lock and enter your password to make changes. Select App Store under the header “Allow apps downloaded from”.


1 Answers

According to Apple's Developer Documentation, you can use built in popovers on OS X with the built-in NSPopover class:

Starting in OS X v10.7, AppKit provides support for popovers by way of the NSPopover class. A popover provides a means to display additional content related to existing content on the screen. The view containing the existing content—from which the popover arises—is referred to in this context as a positioning view. You use an anchor to express the relation between a popover and its positioning view.

Here's a link to the NSPopover class. You can also see an example of NSPopovers used in the Calendar (10.7+) app, and the Safari app (10.8+). The image below depicts a popover in the Calendar app (left) and Safari (right):

Use of NSPopover in Mac OS X


Here's how to setup an NSPopover, it is very simple and can be done mostly in interface builder.

  1. Add an NSPopover Item to your XIB in interface builder. This will create the NSPopover and its view controller.
  2. Next, drag a custom NSView into your XIB through interface builder. This will be the view for the popover view controller
  3. Customize your NSView with any controls you need in your popover
  4. In your header file (.h) add the following two lines of code:

    @property (assign) IBOutlet NSPopover *popover;
    - (IBAction)showPopover:(id)sender;
    

    Don't forget to connect both the outlet and the action to your interface.

  5. In your implementation, synthesize the popover and add the method for showPopover
  6. In the showPopover method, add this line to show the popover:

    [[self popover] showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
    

It's up to you to figure out how to dismiss the popover; because what fun it copy / pasting? You can either do it manually (hint: try using close) or change the behavior property and have the system do it (see the edit below).

Good luck, hope this helps!


Edit

As noted by David in his comment:

Another possibility for dismissing the popover is to set its behavior to Transient. This allows the user to click anywhere outside the popover to have it disappear

The behavior property of a popover sets how it appears and disappears. There are three behaviors:

  • NSPopoverBehaviorApplicationDefined - (Default) Your app must close the popover itself
  • NSPopoverBehaviorTransient - The popover is closed when any interface element is interacted with outside the popover
  • NSPopoverBehaviorSemitransient - The popover is closed when any interface element in the popover's presenting view is interacted with outside the popover.

Read more about this in Apple's Documentation.

like image 100
Sam Spencer Avatar answered Oct 27 '22 22:10

Sam Spencer