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?
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.
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.
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”.
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):
Here's how to setup an NSPopover, it is very simple and can be done mostly in interface builder.
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.
showPopover
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With