I have an UIImageView with some fixed rect size. But my images are not fixed in size. I want to display images according to UIImageView\'s rect size. Whether image is big or
Worth noting that swift 3 has changed as following
.ScaleAspectFit
to scaleAspectFit
.ScaleAspectFill
to .scaleAspectFill
Here a solution for swift:
let imageView = UIImageView(image: UIImage(named: "backgroundImage"))
imageView.frame = tableView.frame
imageView.contentMode = .ScaleAspectFill
tableView.backgroundView = imageView
If you want to make your UIImageView
size according to ratio ,then you must set it's mode to UIViewContentModeScaleAspectFit
yourimage.contentMode=UIViewContentModeScaleAspectFit;
let me know it is working or not!!!
Happy Coding.
In swift you set content mode as well
var newImgThumb : UIImageView
newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
newImgThumb.contentMode = .ScaleAspectFit
Please try this code, Hope it will work for you.
set UIImageView
contentMode
to UIViewContentModeScaleAspectFill
as below :
imageView.contentMode = UIViewContentModeScaleAspectFill;
set autoresizingMask of UIImageView as below :
imageView.autoresizingMask =
( UIViewAutoresizingFlexibleBottomMargin
| UIViewAutoresizingFlexibleHeight
| UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleRightMargin
| UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleWidth );
You just need these two lines:
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.layer.clipsToBounds = YES; // Must do this or else image will overflow