开发者

Can we have popovers in Mac Cocoa apps?

开发者 https://www.devze.com 2023-02-02 22:44 出处:网络
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.

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开发者_如何学Python to accomplish the overlay or could I also use a normal view?


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):

Can we have popovers in Mac Cocoa apps?


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.


If I understood you correctly, you want something like MAAttachedWindow (by Matt Gemmell), which is open source.

Can we have popovers in Mac Cocoa apps?


Alternatively, you can take a look at Popover example in the documentation. https://developer.apple.com/library/mac/samplecode/Popover/Introduction/Intro.html

Can we have popovers in Mac Cocoa apps?

0

精彩评论

暂无评论...
验证码 换一张
取 消