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
in swift 2.2 worked for me:
var OrignTxtNomeCliente:CGPoint!
if let orign = TXT_NomeCliente.superview, let win = UIApplication.sharedApplication().keyWindow {
OrignTxtNomeCliente = orign.convertPoint(TXT_NomeCliente.frame.origin, toView: win)
}
Here is Swift 3 update of @Pablo's answer, which off course worked great in my case.
if let window = UIApplication.shared.keyWindow {
parent.convert(child.frame.origin, to: window)
}
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
var p = button.convert(button.center, to: self.view)
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)
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