ios 设置 UIImageView 的 contentMode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24109114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set contentMode of UIImageView
提问by Ríomhaire
In Obj-C
在 Obj-C 中
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.contentMode = UIViewContentModeScaleAspectFill;
would set the contentMode.
将设置 contentMode。
Why does
为什么
imageView.contentMode = UIViewContentModeScaleAspectFill
imageView.contentMode = UIViewContentModeScaleAspectFill
not work in Swift?
不能在斯威夫特工作?
回答by BergQuester
Somewhat confusingly, Swift drops the prefix for ObjC enum values:
有点令人困惑的是,Swift 删除了 ObjC 枚举值的前缀:
imageView.contentMode = .scaleAspectFill
This is because Swift already knows what enum type is being used. Alternatively, you can specify the enum too:
这是因为 Swift 已经知道正在使用什么枚举类型。或者,您也可以指定枚举:
imageView.contentMode = UIViewContentMode.scaleAspectFill
Note: In versions of Swift before version 3, "scaleAspectFill" will need to be capitalized.
注意:在版本 3 之前的 Swift 版本中,“scaleAspectFill”需要大写。
回答by Ali Raza
In swift language we can set content mode of UIImage view like
在 Swift 语言中,我们可以设置 UIImage 视图的内容模式,例如
var newImgThumb : UIImageView
newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
newImgThumb.contentMode = .ScaleAspectFit
回答by Tommie C.
tldr;
tldr;
See the code answer for Swift 3 at the bottom.
请参阅底部的 Swift 3 代码答案。
Note- Please comment if more information is needed.
注意- 如果需要更多信息,请发表评论。
Please checkout the longer answer below which includes how to use the solution for setting all other properties within your Storyboard or Xib/Nib file.
请查看下面更长的答案,其中包括如何使用解决方案来设置 Storyboard 或 Xib/Nib 文件中的所有其他属性。
There's nothing wrong with the other answers but I want to share how setting values on objects can be done in Interface Builder. I know the OP is asking for code, this example is just being shared for completeness. Of course if one wanted to animate property changes or needed the code versions then the other answers continue to apply.
其他答案没有任何问题,但我想分享如何在 Interface Builder 中设置对象的值。我知道 OP 要求提供代码,此示例只是为了完整性而共享。当然,如果想要动画属性更改或需要代码版本,则其他答案继续适用。
Within Interface Builder
在界面生成器中
- Select the ImageView (or any other control that has an embedded imageView)
- Check the base type of the attribute you want to set (in the case of contentMode this is UIViewContentMode)* see NB for how to...
- Note the type of valid values that can be assigned to the base type (in this case contentMode values correspond to a number)
- Go to the attributes inspector and see the User Defined Runtime Attributes (see image)
- Add a user defined attribute of type Numberand the property name that you want to set (in this case it would be contentMode)
- 选择 ImageView(或任何其他具有嵌入 imageView 的控件)
- 检查要设置的属性的基本类型(在 contentMode 的情况下,这是 UIViewContentMode)* 请参阅 NB 了解如何...
- 请注意可以分配给基本类型的有效值的类型(在这种情况下 contentMode 值对应于一个数字)
- 转到属性检查器并查看用户定义的运行时属性(见图)
- 添加Number类型的用户定义属性 和要设置的属性名称(在本例中为contentMode)
NB - An easy way to explore the underlying types of properties is to Cmd+Click on the property in your source code editor, then Cmd+Click on the type for that property.
注意 - 探索基础属性类型的一种简单方法是在源代码编辑器中 Cmd+单击属性,然后 Cmd+单击该属性的类型。
Here is a simple example where I set some properties for a UIButton, which includes a UIImageView as one of its subviews. The example shows how one can set properties on the top object (UIButton) and the sub object (UIImageView).
这是一个简单的示例,我为 UIButton 设置了一些属性,其中包含一个 UIImageView 作为其子视图之一。该示例展示了如何在顶部对象 (UIButton) 和子对象 (UIImageView) 上设置属性。
If you have an imageView selected then just set the User Defined Runtime Attribute to contentMode of type Number and whatever value you want. This is a good method because it will work for both Objc and Swift.
如果您选择了一个 imageView,那么只需将 User Defined Runtime Attribute 设置为 Number 类型的 contentMode 和您想要的任何值。这是一个很好的方法,因为它适用于 Objc 和 Swift。
What's great is that you can use the same method to capture many other static property values for anything that appears within Interface Builder.
很棒的是,您可以使用相同的方法为 Interface Builder 中出现的任何内容捕获许多其他静态属性值。
Documented values for the UIViewContentMode enum
BTW - Swift 3 changes the enum values to begin with a lower case so the following would work in Swift 3:
顺便说一句 - Swift 3 将枚举值更改为以小写开头,因此以下内容适用于 Swift 3:
imageView.contentMode = .scaleAspectFill
回答by Tasnim Alam Shovon
In Swift 4 it is
在 Swift 4 中,它是
imageView.contentMode = UIView.ContentMode.scaleAspectFit
回答by Dipak Dhondge
In Swift Language we can set imageView in textField as given below.
在 Swift 语言中,我们可以在 textField 中设置 imageView ,如下所示。
let RightImageView = UIImageView()
RightImageView.image = image
let RightView = UIView()
RightView.addSubview(RightImageView)
RightView.frame = CGRectMake(0, 0, 30,30)
Give color to view & imageView So that you can check your added imageView position in textField
为视图和图像视图提供颜色,以便您可以在 textField 中检查添加的图像视图位置
RightView.backgroundColor = UIColor.redColor()
RightImageView.backgroundColor = UIColor.blueColor()
RightImageView.contentMode = UIViewContentMode.ScaleAspectFill
RightImageView.frame = CGRectMake(0, 0,30,30)
textFieldForCountry.rightView = RightView