cocoa-touch - 创建自定义 UIButton

标签 cocoa-touch uikit uibutton

我正在寻找以编程方式创建一个自定义 UIButton(不是子类),我希望它有一个随着 UIButton 宽度增加而拉伸(stretch)的背景图像。我该怎么做?

最佳答案

首先创建一个可拉伸(stretch)的UIImage (假设按钮图像末尾的“上限”为 10px):

UIImage *backgroundImage = [[UIImage imageNamed:@"ImageBackground.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];

然后使用可拉伸(stretch)的图像作为背景:
[myButton setBackgroundImage:backgroundImage forState:UIControlStateNormal];

当您在 UIButton 上设置 backgroundImage 时对于 UIControlStateNormal ,它充当所有状态的背景,除非您为任何其他状态显式设置不同的 backgroundImage。

关于cocoa-touch - 创建自定义 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2707958/

相关文章:

iphone - ScrollView 中的 UIWebView 不会重绘

iphone - 使用 UIButton 玩游戏并制作动画?

swift - 来自单个 UIButton 的两个 "Different"按钮?

objective-c - 裁剪 CAShapeLayer 检索外部路径

ios - 选项卡上按钮图像更改的动画(可能是翻转动画)

ios - objective-c - 获取指向对象属性的指针

ios - 如何判断 UITableViewCell 是否被拖动?

ios - 使用 uidatepicker 设置文本字段值

ios - 当 UITextView 的内容更多时,文本没有出现?

ios - 关闭已由呈现 View Controller 呈现的呈现 View Controller