Stretching, Redrawing and Positioning with contentMode

The contentMode property of UIView allows you to control how to layout a view when the view’s bounds change. The system will not, by default, redraw a view each time the bounds change. That would be wasteful. Instead, depending on the content mode, it can scale, stretch or pin the contents to a fixed position.

If you have trouble remembering what the different content modes do and which you should use this post should help. Not sure how ScaleAspectFit differs from ScaleAspectFill? When might you want to use clipToBounds or switch to Redraw mode? Hopefully this post will tell you all you need to know.

Scaling, Redrawing and Positioning

There are thirteen different content modes but it is easiest to think of three main groups based on the effect:

Scaling the View

There are three modes that have the effect of scaling or stretching the view contents to fill the available space when the bounds changes:

(I will list the Swift enum value first followed by the Objective-C value)

The main difference between these modes is whether and how we maintain the aspect ratio of the contents when scaling. Take the example where we have a UIImageView of a star that we add as a subview of a container view shown in green below:

Content Mode Center

The star image view is 100x100 and the larger container view is 200x350. To experiment with the contentMode we can add constraints to make the star image view fill the container view. The difference content modes then control how we make the image view fill the available space.

The default if you do not set a content mode is ScaleToFill shown below left. ScaleToFill stretches the view to fill the available space without maintaining the aspect ratio.

starImageView.contentMode = .ScaleToFill

Compare this mode with ScaleAspectFit shown below right. ScaleAspectFit scales the content to fit the view but maintains the aspect ratio. Any part of the view bounds that is not filled with content is transparent which allows us to see the green super view in this example.

starImageView.contentMode = .ScaleAspectFit

Content Mode Scale

The final scaling mode is ScaleAspectFill shown below. ScaleAspectFill scales the content to totally fill the view maintaining the aspect ratio. This can result in the content being larger than the bounds of the view.

starImageView.contentMode = .ScaleAspectFill

You can see this below left where the square image view is scaled to 350x350 to fill the 200x350 bounds. If you do not want to see the contents that are outside the bounds you need to remember to set clipsToBounds to true on the containing superview. You can see that below right:

greenView.clipsToBounds = true

Content Mode Clip

Positioning the View

If you do not want to scale or stretch the view with the bounds you can pin it to one of nine possible positions.

Content Mode Center

Content Mode Positions

Content Mode Corners

Note that the contents of the view are not scaled or stretched in any way.

Redrawing the View

Finally, I started by saying that the system does not by default redraw a view when the bounds change. But what if that is what we want?

Perhaps we have a custom view that implements drawRect and we need it called any time the bounds change? The Redraw content mode triggers the setNeedsDisplay method when the view bounds change.

starImageView.contentMode = .Redraw

Getting the Playground

You can get this post as an Xcode playground from my GitHub repository.

Never miss a post!

iOS Size Classes Cheat Sheet

Subscribe and get my free iOS Size Classes Cheat Sheet

Success! Now check your email to confirm your subscription and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
No time to watch WWDC videos?

Sign up to get my iOS posts direct to your inbox and I will send you a free PDF of my iOS Size Classes Cheat Sheet.

OK! Check your inbox (or spam folder) for an email to confirm your details and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
Archives Categories