2012-04-24 72 views
11

我需要获取NSView相对于屏幕的边框/边界。换句话说,我需要x和y坐标是屏幕上的位置,而不是相对于超级视图的位置。在Mac OS X上获取NSView相对于屏幕的边框/边界10.6

我根据评论提出了以下解决方案。

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; 

回答

21
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 2012-04-24 03:36:49