将 UIImage 放入 UIButton 的简单方法
我的 iPhone 应用程序中有一个 UIButton。我将其大小设置为 100x100。
我有一个 400x200 的图像,我希望在按钮中显示它。
按钮仍然需要保持在 100x100...并且我希望图像缩小以适应...但是 保持正确的纵横比。
我想这就是“Aspect Fit”的用途。
我是否将我的图像包含在 setImage 或 setBackgroundImage 中?
我是否为按钮设置了 AspectFit?或图像?或者背景图片?
(我需要较小的图像来增加尺寸。而较大的图像应该减小尺寸。 始终将按钮保持在 100x100。)
我已经尝试了上述所有组合的无数种组合......但我似乎无法得到 一切同时工作:
- 不要更改按钮的大小 100x100。
- 不要破坏图像的纵横比。
- 始终增加小图像以适合按钮。
- 始终缩小大图像以适合按钮。
- 切勿剪掉任何图像边缘。
- 永远不要需要“将 UIButtons 放在所有 UIimages 上”技巧。
- 不需要每个人都升级到v4.2.1只是为了使用新的框架方法。
我看到了这么多的应用程序,有这么多功能齐全的图像按钮……我不敢相信我无法弄清楚这个非常简单、非常常见的事情。
啊。
I have a UIButton in my iPhone app. I set its size to 100x100.
I have an image that is 400x200 that I wish to display in the button.
The button STILL needs to stay at 100x100... and I want the image to downsize to fit... but
keep the correct aspect ratio.
I thought that's what "Aspect Fit" was used for.
Do I include my image with setImage, or setBackgroundImage?
Do I set AspectFit for the button? or the image? or the background image?
(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:
- 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.
Ugh.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
UIButton 已损坏。这就是简短的答案。其 image 和 backgroundImage 属性中的
UIImageView
不遵守UIViewContentMode
设置。它们是只读属性,虽然这些 UIImageView 的UIImage
内容可以通过setImage:
和setBackgroundImage:
方法设置,但内容模式不可以。解决方案是首先在包中提供适当大小的图像,或者放置一个 UIImageView,按照您想要的方式配置它,然后在其顶部放置一个清晰的“自定义”UIButton。我保证,这就是您见过的所有那些精美的专业应用程序都使用过的技巧。我们都必须这样做。
UIButton is broken. That's the short answer. The
UIImageView
s in its image and backgroundImage properties don't respectUIViewContentMode
settings. They're read-only properties, and while theUIImage
contents of those UIImageViews can be set throughsetImage:
andsetBackgroundImage:
methods, the content mode can't be.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.
为了正确地做到这一点,我实际上会以编程方式调整图像大小并操作图像以获得所需的宽高比。这避免了对任何视图层次结构黑客的需要,并且还减少了对单个操作(而不是每次重绘)的任何性能影响。
这段(未经测试的)代码应该有助于说明我的意思:
如果其中任何部分不清楚,请告诉我。
To do this correctly, I would actually programmatically resize and manipulate the image to get the desired aspect ratio. This avoids the need for any view hierarchy hacks, and also reduces any performance hit to a single operation, instead of every redraw.
This (untested) code should help illustrate what I mean:
Let me know if any part of that is unclear.
我认为 Dan 想说的(但从未说过)是这样做:
I think what Dan is trying to say (but without ever saying it) is to do this:
因为我的尝试都没有成功……
也许我应该问这个。使用 UIButton 时:
当我使用 setImage 而不是 setBackgroundImage 时? (为什么两者都有?)
当DO我使用“Aspect Fit”而不是“Center”时? (当我期望它们分别“保持纵横比”和“不调整任何大小”时,为什么两者似乎都拉伸了我的图像。)
最大的问题是:为什么如此常见的事情......如此巨大的混乱?
如果我能找到一种解决方法,例如:只需使用 UIImage 并检测 TAPS,那么这一切都可以解决。 (但这似乎是一个更大的代码噩梦。)
Apple,如果你试图让我的工作变得更容易......你反而让它变得更加混乱 400 倍。
Since none of my attempts have worked....
Maybe I should be asking this instead. When using a UIButton:
When DO I use setImage instead of setBackgroundImage? (Why are there both?)
When DO I use "Aspect Fit" instead of "Center"? (Why do both seem to stretch my images when I expect them to "keep aspect ratio" and "don't resize anything", respective.)
And the big question: Why is such a common thing... such a huge mess?
It would all be solved if I could find a work-around method like: Just use UIImage instead and detect TAPS. (But that seems to be even a LARGER nightmare of code.)
Apple, if you've tried to make my job easier... you have instead made it 400 times more confusing.
将图像视图放在按钮上,为图像视图而不是按钮设置图像。
一切顺利。
Place a imageview over the button, set your image for the imageview and not for button.
All the best.
我会将图像大小调整为 100x100,保持图像中包含的内容的纵横比。然后将 UIButton 的 backgroundImage 属性设置为图像。
I would resize the image to 100x100 maintaining the aspect ratio of the content contained in the image. Then set the backgroundImage property of the UIButton to the image.
几天前我遇到了同样的问题并解决了它。请尝试用这个
I faced same issue few days back and resolved it. Please try with this