Setting content mode for something besides UIImage

2019-07-18 08:16发布

问题:

This answer shows the various effects of setting the content mode of a UIImageView.

Since UIView also has a content mode setting in the Interface Builder, I want to know why setting it doesn't affect any subviews that are in it.

Here is an example that I tried. The blue view is a UIView and the yellow view is a UILabel.

But if I set the mode to Top for the UIView, the label just stays in the same place.

Does content mode only work for UIImageViews? If it doesn't work for UIViews, why is it even there?

回答1:

The contentMode modifies only the views' contents and not its subviews'. So if you have a custom view and you draw something in its drawRect then you will see that it gets changed every time you change the contentMode of your view.

Now in case of UIImageView, the image is actually the content of the view (which is internally drawn in its drawRect), so you can see the image changing when you change the contentMode of your UIImageView.