简体   繁体   中英

Image won't fit in scale to fill mode in image view

It's a really simple task but somehow it just did not work for me. This is what I did:

  1. In storyboard, add an image view to my controller, pin each side to superview, with 0 distance.
  2. Set the image of the image view to be a PNG image which is 646 x 1140.
  3. Set the mode to scale to fill

Then in my simulator, the view controller looks like this:

错误的屏幕截图

As the screenshot shows, there are white spaces around the image.

I can confirm the image itself does not have white spaces around it, and the image view pins well to the four sides of its superview. So why does it look like this? Did I miss anything?

可能是您的图像从左到右一点透明,如果白色也会改变,则尝试更改veiw的颜色,然后它将确认。

set your image as

yourimage.contentMode = UIViewContentModeScaleAspectFit;

some other variation using as per apple documentation

 typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};

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.

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