Justin Meiners
Justin Meiners

Reputation: 11113

How to find absolute NSView frame position in window

I have a complicated hierarchy of NSViews, how can I find the frame of a view in the window or be able to check if a point in the window is in the views frame?

I tried something like this

NSPoint windowOrigin = [[window contentView] convertPoint:NSMakePoint(0,0) fromView:myView];

Upvotes: 2

Views: 3499

Answers (1)

Costique
Costique

Reputation: 23722

Send convertRect:toView: with the second argument set to nil:

NSRect viewFrameInWindowCoords = [myView convertRect: [myView bounds] toView: nil];

On a second thought, if you need to perform hit testing, you might want to do the opposite:

NSView *viewUnderPoint = [[window contentView] hitTest: locationInWindow];

Upvotes: 8

Related Questions