Milktea Milktea - 1 month ago 5x
Swift Question

How do you obtain the actual rendered value programmatically for an item that's positioned through autolayout?

I currently have a logo horizontally centered on the storyboard, vertically positioned 50 pixels from the top of the story board, and it has a set width of 377 and a set height of 200. I am trying to set the bounds for another element programmatically using an IBOutlet tied to the logo, but the bounds that are being returned aren't the proper values, at least from what I've set in the story board.

I expect the values returned from logo.frame to be something like

x = 100, y = 50, width = 377, height = 200

but the actual values are

x = 0, y = 0, width = 240, height = 128

Is this because the logo has a dynamic positioning? Is there a different object I can use to get the values as they're rendered on the screen?


Have you tried to fetch the position after the layout has been changed, for example in viewDidAppear() ?

override func viewDidAppear(_ animated: Bool) {
    let width = logo.frame.width