简体   繁体   English

在iOS中为UIImageView以外的内容设置内容模式

[英]Setting content mode for something besides UIImageView in iOS

This answer shows the various effects of setting the content mode of a UIImageView . 此答案显示了设置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. 由于UIView在Interface Builder中也具有内容模式设置,因此我想知道为什么设置它不会影响其中的任何子视图。

Here is an example that I tried. 这是我尝试的示例。 The blue view is a UIView and the yellow view is a UILabel . 蓝色视图是UIView ,黄色视图是UILabel

在此处输入图片说明

But if I set the mode to Top for the UIView , the label just stays in the same place. 但是,如果我将UIView的模式设置为Top,则标签将停留在同一位置。

在此处输入图片说明

Does content mode only work for UIImageViews ? 内容模式仅适用于UIImageViews吗? If it doesn't work for UIViews , why is it even there? 如果不适用于UIViews ,为什么UIViews那里呢?

The contentMode modifies only the views' contents and not its subviews'. contentMode仅修改视图的内容,而不修改其子视图。 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. 因此,如果您有一个自定义视图,并在其drawRect绘制了一些内容,则每次更改视图的contentMode都会看到它被更改。

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. 现在,在UIImageView情况下,图像实际上是视图的内容(在其内部的drawRect绘制),因此,当您更改UIImageView的contentMode时,可以看到图像发生了变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM