我有一个动态 UIImage 和一个固定宽度为 280.0px 的 UIImageView,并且我正在使用自动布局。在 UIImage 视图上,我设置了宽度和高度约束,并降低了高度约束的优先级。我选择了“aspect fit”,并将内容拥抱和压缩优先级提高到 1000。
在这种情况下,UIImageView 会调整大小以保持比例,但高度比我想要的要大。由于 UIImageView 的顶部和底部有空白区域,因此保留了纵横比。我想删除这些空间并自动调整高度以完美适合 UIImage。
像这样的东西...
UIImage *image = ...;
UIImageView *imageView = ...;
imageView.image = image;
float ar = image.size.width/image.size.height;
NSLayoutConstraint* aspectConstraint = [NSLayoutConstraint constraintWithItem: imageView
attribute: NSLayoutAttributeWidth
relatedBy: NSLayoutRelationEqual
toItem: imageView
attribute: NSLayoutAttributeHeight
multiplier: ar
constant: 0];
[parentView addConstraint: aspectConstraint];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)