在Mac OS X 10.6上获取NSView相对于屏幕的frame/bounds

13

我需要获取一个NSView相对于屏幕的frame/bounds。换句话说,我需要x和y坐标是在屏幕上的位置而不是相对于其父视图的位置。

我根据评论提供的解决方案如下。

NSRect frameRelativeToWindow = [self.view
    convertRect:self.view.bounds toView:nil
];
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_6
NSPoint pointRelativeToScreen = [self.view.window
    convertRectToScreen:frameRelativeToWindow
].origin;
#else
NSPoint pointRelativeToScreen = [self.view.window
    convertBaseToScreen:frameRelativeToWindow.origin
];
#endif

NSRect frame = self.view.frame;

frame.origin.x = pointRelativeToScreen.x;
frame.origin.y = pointRelativeToScreen.y;
1个回答

25
NSRect frameRelativeToWindow = [myView convertRect:myView.bounds toView:nil];
NSRect frameRelativeToScreen = [myView.window convertRectToScreen:frameInWindow];

3
convertRectToScreen 可在 Mac OS X v10.7 及更高版本中使用。我需要使用 10.6 SDK。 - junglecat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接