1

I have a UIButton that is nested within 2 UIStackViews that are nested within a UIView.

enter image description here

I give it constraints, however, when I

print(helloButton.frame)

the CGRect output has different values than what appears on the screen.

Notably the coordinates are off.

Is there a reason for this? How do I accurately get the UIButton frame?

Eric Johnson
  • 1,087
  • 1
  • 11
  • 16

1 Answers1

0

Answering my question. The coordinate origin of a UIView.frame CGRect at any time is based on the origin of its containing UIView, not that of the application's view.

To obtain its coordinates based on the origin of the application (absolute top-left corner) I did this:

In Swift:

let absoluteFrame = myView.convertRect(myView.frame, toView: nil)
Eric Johnson
  • 1,087
  • 1
  • 11
  • 16
  • 1
    You also should make this conversion in `viewDidLayoutSubviews`, not in `viewDidAppear` (layout may not have finished in `viewDidAppear`) – Aaron Brager Dec 15 '15 at 03:08