Ian Vink
Ian Vink

Reputation: 68750

iOS: Centering an UIImageView in a wide UIView

I have a small UIImageView and wish to centre it horizontally inside a UIView which is as wide as the screen.

I also wish to have the UIImageView stay centered when the device rotates.

How is this done?

Upvotes: 1

Views: 4200

Answers (1)

XJones
XJones

Reputation: 21967

This will center imageView horizontally within view:

imageView.frame.origin.x = CGRectGetMidX(view.bounds) - CGRectGetMidX(imageView.bounds)

This will ensure that imageView stays horizontally centered when its superview's bounds change:

imageView.autoresizingMask = UIViewAutoResizingFlexibleLeftMargin|UIViewAutoResizingFlexibleRightMargin

Upvotes: 3

Related Questions