Brian Ogden
Brian Ogden

Reputation: 19212

Finding the center of UIImageView UIImageView.center unexpected results

I have a UIImageView with the following constraints:

The width of the UIImageView in Xcode 7.2 Size Inspector = 600.

In my UIViewController have an outlet to the UIImageView:

@IBOutlet weak var heroImageView: UIImageView!

In viewDidLoad the value of heroImagveView.center = (300.0, 67.5)

However this not the actual center of the image, 300 is way to the right of the UIImageView in an iPhone 5 simulator. Something more like (x: 160.0, y: 67.5) is center, though x is not exact.

In Xcode 7.2 the default width for all my UIViewControllers is 600 pixels, I understand that will not be the runtime rendering width across different Apple devices, but why doesn't UIImageView.center understand this?

How to I get the actual runtime center of a UIImageView?

Upvotes: 0

Views: 100

Answers (1)

John Bennedict Lorenzo
John Bennedict Lorenzo

Reputation: 527

At viewDidLoad, your heroImageView has a width of 600. Since the leading space to the superview is 0, the x position will be 0. Hence, the center is at 300.

You need to remove the trailing and leading constraints and replace them with a 'center horizontally', and a width constraint.

Upvotes: 0

Related Questions