Setting content mode for something besides UIImage

2019-07-18 08:22发布

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.

enter image description here

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

enter image description here

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

1条回答
2楼-- · 2019-07-18 09:16

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.

查看更多
登录 后发表回答