In Obj-C
imageView.contentMode = UIViewContentModeScaleAspectFill;
would set the contentMode.
Why does
imageView.contentMode = UIViewContentModeScaleAspectFill
not work in Swift?
Somewhat confusingly, Swift drops the prefix for ObjC enum values:
imageView.contentMode = .scaleAspectFill
This is because Swift already knows what enum type is being used. Alternatively, you can specify the enum too:
imageView.contentMode = UIViewContentMode.scaleAspectFill
Note: In versions of Swift before version 3, "scaleAspectFill" will need to be capitalized.
In swift language we can set content mode of UIImage view like
var newImgThumb : UIImageView
newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
newImgThumb.contentMode = .ScaleAspectFit
tldr;
See the code answer for Swift 3 at the bottom.
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.
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.
Within Interface Builder
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.
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).
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.
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.
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:
imageView.contentMode = .scaleAspectFill
在Swift 4中它是
imageView.contentMode = UIView.ContentMode.scaleAspectFit
In Swift Language we can set imageView in textField as given below.
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
RightView.backgroundColor = UIColor.redColor()
RightImageView.backgroundColor = UIColor.blueColor()
RightImageView.contentMode = UIViewContentMode.ScaleAspectFill
RightImageView.frame = CGRectMake(0, 0,30,30)
textFieldForCountry.rightView = RightView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.