将 UIImage 放入 UIButton 的简单方法 [英] A simple way to put a UIImage in a UIButton

查看:24
本文介绍了将 UIImage 放入 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.

按钮仍然需要保持在 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:

  • 不要将按钮的大小更改为 100x100.
  • 不要破坏图片的纵横比.
  • 始终增加小图片以适应按钮.
  • 始终缩小大图像以适应按钮.
  • 切勿剪裁任何图像边缘.
  • 永远不需要将 UIButtons 放在所有 UIimages 上"的技巧.
  • 不要要求每个人都升级到 v4.2.1 只是为了使用新的框架方法.

我看到这么多应用程序,有这么多功能齐全的图像按钮......我简直不敢相信我无法弄清楚这个非常简单、非常常见的事情.

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 UIImageViews 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆