我正在寻找以编程方式创建一个自定义 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/