iPhone - Get Position of UIView within entire UIWindow

后端 未结 6 1409
长发绾君心
长发绾君心 2020-11-27 09:14

The position of a UIView can obviously be determined by view.center or view.frame etc. but this only returns the position of the

6条回答
  •  醉话见心
    2020-11-27 09:54

    That's an easy one:

    [aView convertPoint:localPosition toView:nil];
    

    ... converts a point in local coordinate space to window coordinates. You can use this method to calculate a view's origin in window space like this:

    [aView.superview convertPoint:aView.frame.origin toView:nil];
    

    2014 Edit: Looking at the popularity of Matt__C's comment it seems reasonable to point out that the coordinates...

    1. don't change when rotating the device.
    2. always have their origin in the top left corner of the unrotated screen.
    3. are window coordinates: The coordinate system ist defined by the bounds of the window. The screen's and device coordinate systems are different and should not be mixed up with window coordinates.

提交回复
热议问题