如何创建自定义UIButton(带有图像)但仍使用setTitle? [英] How to create a custom UIButton (with images) but still use setTitle?
问题描述
我尝试用UIButtonTypeCustom创建一个UIButton.然后使用以下方法调整外观
I've tried creating a UIButton with UIButtonTypeCustom. Then afterwards used the following methods to adjust the look of it
[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?听起来像是很多工作:(有什么想法吗?
However the problem is, once you start setting UIImages to the states, setTitle no longer works. I think setTitle only works with non-UIButtonTypeCustom? This means I either have to put the text into the images themselves (not very robust) or subclass this and add a UILabel to the view? Sounds like alot of work :( Any ideas?
推荐答案
如果希望标题显示在图像上,请尝试使用以下方法:
If you want the title to display over the image, try using this:
[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonOff.png"] forState:UIControlStateNormal];
[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonOn.png"] forState:UIControlStateHighlighted];
[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonDisabled.png"] forState:UIControlStateDisabled];
这篇关于如何创建自定义UIButton(带有图像)但仍使用setTitle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!