在iOS中为UIImageView以外的内容设置内容模式 [英] Setting content mode for something besides UIImageView in iOS

查看:167
本文介绍了在iOS中为UIImageView以外的内容设置内容模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案显示了设置

内容模式仅适用于UIImageViews吗?如果UIViews不能使用,为什么还可以呢?

解决方案

contentMode仅修改视图的内容,而不修改其子视图.因此,如果您有一个自定义视图,并且在其drawRect中绘制了一些内容,那么您每次更改视图的contentMode时都会看到它被更改.

现在,对于UIImageView,图像实际上是视图的内容(在其drawRect中内部绘制),因此,当您更改UIImageView的contentMode时,可以看到图像发生了变化.

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?

解决方案

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.

这篇关于在iOS中为UIImageView以外的内容设置内容模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆