UIButton标题消失
问题描述:
我有一个与UIButton里面的ViewController。 当我点击时,按钮上的文字消失。 我为所有状态添加所有setTitle,但它继续消失。 有什么想法?UIButton标题消失
这是我的代码的一部分:
@interface AddCardViewController : UITableViewController <UITextFieldDelegate>{
UIButton *commit;
......
@implementation AddCardViewController
- (void)viewDidLoad{
self.commit = [UIButton buttonWithType: UIButtonTypeCustom];
[self setCommitProperties];
[self.view addSubview:commit];
.........}
- (void) setCommitProperties{
CGRect frameTable = self.tableView.frame;
CGRect frame = CGRectMake(frameTable.origin.x + 10, 140, frameTable.size.width - 20, 40);
commit.frame = frame;
[commit setBackgroundColor : [UIColor whiteColor]];
[commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateNormal];
[commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateSelected];
[commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateHighlighted];
[commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateApplication];
[commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateReserved];
[commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateDisabled];
[commit addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
UIColor *color = [[[PersonalizationManager alloc] init] getColor: @"AddCardViewController" :@"Commit_Title"];
[commit.titleLabel setTextColor: color];
color = [[[PersonalizationManager alloc] init] getColor: @"AddCardViewController" :@"Commit_Border"];
[commit.layer setBorderColor:[color CGColor]];
[commit.layer setBorderWidth : 0.5f];
[commit.layer setCornerRadius : 10.0f];
}
答
那么它很难肯定地说,但文字不可见的,因为颜色是一样的吗?我看到你设置了所有状态的文本,但是你也可能想要为所有状态设置颜色。
[commit setTextColor:[UIColor redColor] forState:UIControlStateSelected];
答
我猜你需要使用[self.commit setTitle: NSLocal...
,实际使用self.commit整体的分配(当您使用@property (nonatomic, retain)
或strong
与ARC)。我使用@synthesize commit = _commit;
,然后仅对模块的其余部分使用_commit。
答
我注意到一件奇特的事情。 如果我只要我按一下按钮设置使用
bttn.titleLabel.textColor = [UIColor purpleColor];
标题之后,标题消失。 但是,如果我用下面的方法:
[bttn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
然后一切正常。
答
使用属性文本时,确保在storboard/xib中按钮Type = 自定义(不是系统)。
答
正如Jack所说,这是按钮的“已选择”状态,它具有与背景相同的颜色。 您可以更改颜色也是在故事板:
- 选择按钮
- 在属性检查器 - >状态配置 - >选择“突出”
- 选择正确的“文本颜色”
是啊!那是解决方案。你是对的。 我没有考虑颜色,我试过并测试了Title的所有属性。 这些都是问的好东西。 非常感谢 – 2012-02-16 14:48:35
我是一个新的ios开发人员,我认为api已更改cz没有setTextColor方法可用。改用setTitleColor – sudip 2013-01-27 14:43:30