简体   繁体   English

AspectFill无法填充UIImageView

[英]AspectFill does not fill the UIImageView

I have a button that has contentMode set to UIViewContentModeScaleAspectFill . 我有一个将contentMode设置为UIViewContentModeScaleAspectFill的按钮。 I set it like this: 我这样设置:

self.itemImage.imageView.contentMode = UIViewContentModeScaleAspectFill;

I also set it to Mode - Aspect Fill in IB for the button, just in case. 为了防万一,我还将其设置为“模式-方面”。 And when I set the image, I DON'T set is as a background image: 当我设置图像时,我不设置为背景图像:

[self.itemImage setImage:image forState:UIControlStateNormal];

However, occasionally my image does not fill the button (which I want it to do), like in this case: 但是,有时我的图像没有填充按钮(我想要它做),例如在这种情况下:

在此处输入图片说明

See grey spaces on each side. 看到每边的灰色空间。 Occurs on both iOS 6 and 7. 在iOS 6和iOS 7上同时出现。

Any ideas? 有任何想法吗?

At long last, I managed to find the solution that works in Alfie's answer to this SO question . 最后,我设法在Alfie 对此SO问题的答案中找到了可行的解决方案。

In short, if your UIButton (or its hidden imageView ) does not respond to contentMode , use this code: 简而言之,如果您的UIButton (或其隐藏的imageView )不响应contentMode ,请使用以下代码:

self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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