Stretching, Redrawing and Positioning with contentModeApr 4, 2016 · 3 minute read · Comments
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 (with or without maintaining the aspect ratio)
- Redrawing the View
- Positioning the View
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)
- ScaleToFill (UIViewContentModeScaleToFill)
- ScaleAspectFit (UIViewContentModeScaleAspectFit)
- ScaleAspectFill (UIViewContentModeScaleAspectFill)
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:
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
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
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.
- Center (UIViewContentModeCenter)
- Top (UIViewContentModeTop)
- Bottom (UIViewContentModeBottom)
- Left (UIViewContentModeLeft)
- Right (UIViewContentModeRight)
- TopLeft (UIViewContentModeTopLeft)
- TopRight (UIViewContentModeTopRight)
- BottomLeft (UIViewContentModeBottomLeft)
- BottomRight (UIViewContentModeBottomRight)
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?
- Redraw (UIViewContentModeRedraw)
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.