Using convertPoint to get the relative position inside a parent UIView

后端 未结 5 1430
臣服心动
臣服心动 2020-12-07 13:12

I\'ve looked at a dozen SO questions on this topic, and none of the answers have worked for me. Maybe this will help get me back on the right path.

Imagine this set

5条回答
  •  暖寄归人
    2020-12-07 13:57

    Martin answer is correct. For developers using Swift, you can get the position of an object (button, view,...) relative to the screen by using:

    var p = obj.convertPoint(obj.center, toView: self.view)
    
    println(p.x)  // this prints the x coordinate of 'obj' relative to the screen
    println(p.y)  // this prints the y coordinate of 'obj' relative to the screen
    

提交回复
热议问题