NSView里面的NSPopover:转换坐标 [英] NSView inside an NSPopover: Converting Coordinates

查看:644
本文介绍了NSView里面的NSPopover:转换坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文



P 成为NSPopover和视图是一个NSView,它是 P 中包含的子视图。



我需要什么



我想使用视图的框架并将其转换到屏幕的坐标系。通常,你会这样做:

  NSRect frameRelativeToWindow = [self convertRect:self.frame relativeToView:nil]; 
[self.window convertRectToScreen:frameRelativeToWindow];

但是,因为 view ,其窗口属性为nil,上述方法将返回NSZeroRect:[0,0,0,0]



那么,如果视图在NSPopover中,我如何获得我的视图的框架在屏幕坐标?



注意:




  1. NSPopover在屏幕上可见。
  2. 我想要将其帧转换为屏幕坐标的视图不是NSPopover或其contentView的直接子视图。所讨论的视图实际上是NSStripView内的NSScrollView内的NSTableView内的NSTableCellView中的自定义NSView。 clipView是NSPopover的contentView的直接子视图。

  3. 在正常的NSWindow中,可以使用任何子视图并调用 [self window] ,以获取包含NSWindow对象。

  4. 解决方案

    下面的代码假设,您拥有




    • 一个NSViewController子类,用于管理弹出视图

    • 视图控制器实现 NSPopoverDelegate



    正如hamstergene指出的,popover的窗口将不可用,直到

    当调用 popoverWillShow:委托方法时,您的popover视图的窗口应该是 _NSPopoverWindow (而不是 nil )。

      )popoverWillShow:(NSNotification *)notification 
    {
    NSWindow * popOverWindow = self.view.window;
    NSRect popoverRectInScreenCoords = [popOverWindow convertRectToScreen:self.view.frame];
    NSLog(@Popover Rect in Screen Coords:%@,NSStringFromRect(popoverRectInScreenCoords));
    }

    更新

    Bryan (OP)在评论中发布了他的问题的解决方案。

    问题不是 NSPopover 本身。他的一些子视图的 nil 窗口的实际原因是 NSTableView ,它释放不可见的 NSTableCellViews

    作为解决方法,他在他的自定义 NSTableCellView viewDidMoveToWindow c $ c>子类。当调用 viewDidMoveToWindow:时,表单元视图已经有一个窗口,可以用来执行坐标转换。


    Context

    Let P be an NSPopover and view be an NSView that is a subview contained within P.

    What I Need

    I want to take the frame of view and convert it to the screen's coordinate system. Normally, you would do that like this:

    NSRect frameRelativeToWindow = [self convertRect:self.frame relativeToView:nil]; 
    [self.window convertRectToScreen:frameRelativeToWindow];
    

    However, because view is in an NSPopover, its window property is nil and the above method will return NSZeroRect: [0, 0, 0, 0]

    So, how can I get my view's frame in screen coordinates if the view is in an NSPopover?

    Notes:

    1. The NSPopover IS onscreen and visible.
    2. The view whose frame I am trying to convert to screen coordinates is not a direct subview of the NSPopover or its contentView. The view in question is actually a custom NSView within an NSTableCellView within an NSTableView within an NSScrollView within an NSClipView. The clipView is a direct subview of the NSPopover's contentView.
    3. In a normal NSWindow, you can take any subview and call [self window] to get the containing NSWindow object. Doing that in a popover, however, returns nil.

    解决方案

    The following code assumes, that you have

    • a NSViewController subclass that manages the popover view
    • the view controller implements NSPopoverDelegate

    As hamstergene pointed out, the popover's window won't be available until it is shown.
    When the popoverWillShow: delegate method gets called, your popover view's window should be an instance of _NSPopoverWindow (and not nil).

    - (void)popoverWillShow:(NSNotification *)notification
    {
        NSWindow* popOverWindow = self.view.window;
        NSRect popoverRectInScreenCoords = [popOverWindow convertRectToScreen:self.view.frame];
        NSLog(@"Popover Rect in Screen Coords:%@", NSStringFromRect(popoverRectInScreenCoords));
    }
    

    Update
    Bryan (the OP) posted a solution to his problem in the comments.
    The issue wasn't NSPopover itself. The actual reason for the nil window for some of his subviews was NSTableView, which releases non-visible NSTableCellViews.
    As a workaround he implemented viewDidMoveToWindow in his custom NSTableCellView subclass. When viewDidMoveToWindow: gets called, the table cell view already has a window that can be used to perform coordinate conversion.

    这篇关于NSView里面的NSPopover:转换坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆