我的 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.
按钮仍然需要保持在 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?
(我需要较小的图像增大尺寸.而较大的图像应该减小尺寸.始终将按钮保持在 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:
我看到这么多应用程序,有这么多功能齐全的图像按钮......我简直不敢相信我无法弄清楚这个非常简单、非常常见的事情.
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 坏了.这是简短的答案.UIImageView
的 image 和 backgroundImage 属性不遵守 UIViewContentMode
设置.它们是只读属性,虽然这些 UIImageView 的 UIImage
内容可以通过 setImage:
和 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 的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!