UIImageView 内容模式 [英] UIImageView content mode

查看:52
本文介绍了UIImageView 内容模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

蓝线是图像视图的边界.UIImageViewcontentModeUIViewContentModeScaleAspectFit,我想保留原始图片的比例.如何使图片的左边缘位于 UIImageView 的左边缘?但不像 UIViewContentModeTopLeft 模式.保持比例尺,左边不留空白.我的英语不好,希望你们能明白我在说什么.非常感谢.

The blue line is the imageview's bounds. UIImageView's contentMode is UIViewContentModeScaleAspectFit, And I want keep the original picture's scale. How can I make the picture's left edge is on the UIImageView's left edge? But not like UIViewContentModeTopLeft mode. Keep the scale and no blank in the left. My english is not good, hope you guys could understand what I'm saying. Thank you very much.

推荐答案

 yourimageView.contentMode = UIViewContentModeCenter;
if ( yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height) {
   yourimageView.contentMode = UIViewContentModeScaleAspectFit;
}

Swift3

yourimageView.contentMode = .center
if yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height {
yourimageView.contentMode = .scaleAspectFit
}

你可以改变你的模式

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,
};

这篇关于UIImageView 内容模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆