[英]Make a UIButton background image larger than frame
I'm trying to make a UIButton
with a background image where the image is larger than the button frame. 我正在尝试使用背景图像制作UIButton
,其中图像大于按钮框架。
The problem is, the UIButton
automatically scales the image to fit and haven't found a way to change the behavior. 问题是, UIButton
自动缩放图像以适应并且没有找到改变行为的方法。
Am I missing something obvious or is it time to make a custom button? 我错过了一些明显的东西,还是时候制作一个自定义按钮?
Subclass UIButton and override the 子类UIButton并覆盖
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
method so it returns the size you want. 方法,以便它返回您想要的大小。
TIP: Use this method to center your background as well. 提示:使用此方法也可以居中。
Looks like the answer to this one is. 看起来这个问题的答案是。 Not possible through existing interface on UIButton
. 通过UIButton
上的现有界面无法实现。
The simplest way to implement seems to be using a custom sub class of UIButton
and creating a UIImageView
with the same frame as the UIButton
, turn off clipping and scaling and then just capture all the setBackground
methods and capture state changes to handle "normal" button interaction changing the background images. 实现最简单的方法似乎是使用自定义子类UIButton
和创建UIImageView
用相同的帧UIButton
,关闭剪裁和缩放,然后就捕获所有setBackground
方法和拍摄状态的变化来处理“正常”按钮交互改变背景图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.