2

我试过用 UIButtonTypeCustom 创建一个 UIButton。然后用下面的方法来调整它的外观

[sendButton setImage:[UIImage imageNamed:@"sendButtonOff.png"] forState:UIControlStateNormal];
[sendButton setImage:[UIImage imageNamed:@"sendButtonOn.png"] forState:UIControlStateHighlighted];      
[sendButton setImage:[UIImage imageNamed:@"sendButtonDisabled.png"] forState:UIControlStateDisabled];

然而问题是,一旦你开始将 UIImages 设置为状态,setTitle 就不再起作用了。我认为 setTitle 仅适用于非 UIButtonTypeCustom?这意味着我要么必须将文本本身放入图像中(不是很健壮),要么将其子类化并将 UILabel 添加到视图中?听起来工作量很大:(有什么想法吗?

4

1 回答 1

10

如果您希望标题显示在图像上,请尝试使用:

[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonOff.png"] forState:UIControlStateNormal];
[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonOn.png"] forState:UIControlStateHighlighted];      
[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonDisabled.png"] forState:UIControlStateDisabled];
于 2009-07-14T02:21:34.437 回答