我想在各个地方(而不是在导航栏中)放置具有动态文本的按钮。我希望它们看起来类似于黑色导航栏按钮项(带有灰色和黑色渐变)。如何创建这些按钮?它们需要根据按钮文本的长度动态调整宽度。我知道可以创建PNG文件并将其拉伸,但是否有更好的方法?
这样做可能会有帮助:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
int capHeight = 31;
int capWidth = 9;
UIImage *buttonTemplate = [UIImage imageNamed:@"button_template.png"];
UIImage *stretchedButtonImage = [buttonTemplate stretchableImageWithLeftCapWidth:capWidth topCapHeight:capHeight];
[button setBackgroundImage:stretchedButtonImage forState:UIControlStateNormal];
stretchableImageWithLeftCapWidth:topCapHeight:
在iOS5中已被弃用。请改用resizableImageWithCapInsets:
。(如链接页面所述。) - PEZresizableImageWithCapInsets:
选择器,否则使用已弃用的方法。 - PEZ