Using convertPoint to get the relative position inside a parent UIView
Swift 5.2
You need to call convert
from the button, not the superview. In my case I needed width data so I converted the bounds instead of just center point. The code below works for me:
let buttonAbsoluteFrame = button.convert(button.bounds, to: self.view)
button.center
is the center specified within the coordinate system of its superview, so I
assume that the following works:
CGPoint p = [button.superview convertPoint:button.center toView:self.view]
Or you compute the button's center in its own coordinate system and use that:
CGPoint buttonCenter = CGPointMake(button.bounds.origin.x + button.bounds.size.width/2,
button.bounds.origin.y + button.bounds.size.height/2);
CGPoint p = [button convertPoint:buttonCenter toView:self.view];
Swift 4+
let p = button.superview!.convert(button.center, to: self.view)
// or
let buttonCenter = CGPoint(x: button.bounds.midX, y: button.bounds.midY)
let p = button.convert(buttonCenter, to: self.view)