【问题标题】:NSPopover Not AppearingNSPopover 没有出现
【发布时间】:2012-07-15 19:49:00
【问题描述】:

我真的希望我可以更具体,但不幸的是,我不能。所有适当的对象都是非零,并且所有适当的方法调用都(可能)成功地进行,但我的 NSPopover 只是从未出现过。也没有调用任何委托方法。

// ivars
NSPopover *tagPopover;
NSViewController *tagPopoverViewController;

// in method to display popover
tagPopover = [[NSPopover alloc] init];
[tagPopover setBehavior: NSPopoverBehaviorApplicationDefined];
[tagPopover setDelegate: self];
tagPopoverViewController = [[MYViewController alloc] initWithNibName: @"MYViewController" bundle: nil];
[tagPopover setContentViewController: tagPopoverViewController];
[tagPopover setContentSize: tagPopoverViewController.view.frame.size];

[tagPopover showRelativeToRect: NSMakeRect(700, 400, 5, 5) // Screen coordinates
                        ofView: [[NSApp keyWindow] contentView]
                 preferredEdge: NSMinYEdge];

【问题讨论】:

  • @Monolo,是的,-awakeFromNib 很好。

标签: cocoa coordinates popover


【解决方案1】:

原来坐标系错了:

NSRect theRect = [[NSApp keyWindow] convertRectFromScreen: NSMakeRect(700, 400, 5, 5)];
[tagPopover showRelativeToRect: theRect // Window Coordinates
                        ofView: [[NSApp keyWindow] contentView]
                 preferredEdge: NSMinYEdge];

需要先转换成窗口的坐标系。

【讨论】:

    【解决方案2】:

    我怀疑tagPopoverViewController.view.frame 是 NSZeroRect,因为框架在它们开始出现在屏幕上之前已设置为该值(例如,它们从 xibs/storyboards 出来时毫无意义)。你确定你需要那条线吗?文档提到 contentSize 无论如何都绑定到内容视图的大小。

    【讨论】:

    • 或者,有点肮脏的 hack,在初始化后立即在视图控制器上调用 view 以强制加载视图。
    • 不:tagPopoverViewController.view.frame: {{0, 0}, {281, 118}}
    • @Monolo 行tagPopoverViewController.view.frame.size 确实调用视图;-)
    猜你喜欢
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多