将UIImage放入UIButton的简单方法 [英] A simple way to put a UIImage in a UIButton
问题描述
我的iPhone应用程序中有一个UIButton。我将其大小设置为100x100。
I have a UIButton in my iPhone app. I set its size to 100x100.
我有一个400x200的图像,我希望在按钮中显示。
I have an image that is 400x200 that I wish to display in the button.
按钮STILL需要保持在100x100 ......我希望图像缩小以适应......但
保持正确的宽高比。
The button STILL needs to stay at 100x100... and I want the image to downsize to fit... but keep the correct aspect ratio.
我认为这就是Aspect Fit的用法。
I thought that's what "Aspect Fit" was used for.
我是否用setImage或setBackgroundImage包含我的图像?
Do I include my image with setImage, or setBackgroundImage?
我是否为按钮设置了AspectFit?还是形象?或者背景图片?
Do I set AspectFit for the button? or the image? or the background image?
(我需要较小的图像来增加大小。虽然较大的图像应该是DESCREASE的大小。
始终保持按钮为100x100。 )
(I need the smaller images to INCREASE in size. While larger images should DESCREASE in size. Always keeping the button at 100x100.)
我尝试了以上所有的无数组合...而我似乎无法获得
同时工作的一切:
I've tried countless combinations of all of the above... and I just can't seem to get everything to work at the same time:
- 请勿将按钮的大小更改为100x100。
- 不要破坏图像的宽高比。
- 始终增加小图像以适应按钮。
- 始终减小大图像以适应按钮。
- 永远不要剪辑任何图像边缘。
- 永远不要求将UIButtons放在所有UIimages上黑客攻击。
- 不要要求大家升级到v4.2.1只是为了使用新的框架方法。
- Don't change the button's size of 100x100.
- Don't destroy the image's aspect ratio.
- Always increase small images to fit the button.
- Always decrease large images to fit the button.
- Never clip any of the image edges.
- Never require the "put UIButtons over all your UIimages" hack.
- Don't require everyone to upgrade to v4.2.1 just to use new framework methods.
我看到很多应用程序,有这么多全功能图像按钮...我无法相信我无法弄清楚这个非常简单,非常常见的事情。
I see so many apps, with so many fully-working image-buttons... that I can't believe I can't figure out this very simple, very common thing.
呃。
推荐答案
UIButton坏了。这是简短的答案。其image和backgroundImage属性中的 UIImageView
不尊重 UIViewContentMode
设置。它们是只读属性,而这些UIImageViews的 UIImage
内容可以通过 setImage:
和<设置code> setBackgroundImage:方法,内容模式不能。
UIButton is broken. That's the short answer. The UIImageView
s in its image and backgroundImage properties don't respect UIViewContentMode
settings. They're read-only properties, and while the UIImage
contents of those UIImageViews can be set through setImage:
and setBackgroundImage:
methods, the content mode can't be.
解决方案是提供适当大小的图像你的软件包开始,或者放下UIImageView,按照你想要的方式配置它,然后在它上面放一个清晰的自定义UIButton。我承诺,这就是你见过的那些花哨的专业应用程序。我们都必须这样做。
The solution is either to provide properly-sized images in your bundle to begin with, or to put a UIImageView down, configure it the way you want it, and then put a clear "custom" UIButton over top of it. That's the hack all those fancy professional apps you've seen have used, I promise. We're all having to do it.
这篇关于将UIImage放入UIButton的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!