简体   繁体   English

UIButton标题没有出现

[英]UIButton title doesn't show up

I'm having a little problem with a UIButton I try to create programmatically. 我尝试以编程方式创建UIButton时遇到一些问题。 The problem is that the button shows up (ie if I set a background color, I can clearly see the button) and is clickable, but the title label isn't visible. 问题是按钮显示(即,如果我设置背景颜色,我可以清楚地看到按钮)并且是可点击的,但标题标签不可见。

Here's what I'm doing : 这是我正在做的事情:

valueButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [valueButton addTarget:self action:@selector(openList:) forControlEvents:UIControlEventTouchUpInside];
    valueButton.frame = CGRectMake(160.0, decalageLabel+6.0, 120.0, 20.0);

    [valueButton.titleLabel setTextAlignment:UITextAlignmentRight];
    [valueButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Neue-Light" size:17.0]];
    UIColor * colorLabelValue = [[UIColor alloc] initWithRed:131.0/255.0 green:159.0/255.0 blue:86.0/255.0 alpha:1.0] ;

    [valueButton setTitleColor:colorLabelValue forState:UIControlStateNormal];

    if(txtValueField == NULL){
        txtValueField = @"";
    }
    valueButton.titleLabel.text = [NSString stringWithFormat:@"%@", txtValueField];
    NSLog(@"button : %@ ", valueButton.titleLabel.text);
    [self.contentView addSubview:valueButton];

Oh and I've made sure the title had an actual value, by NSLogging its text value, which works fine. 哦,我确保标题有一个实际值,通过NSLogging它的文本值,这很好。 So I don't know what's happening. 所以我不知道发生了什么。 Any idea? 任何想法?

Don't use [[button titleLabel] setTitle:] for this. 不要使用[[button titleLabel] setTitle:] Use [button setTitle:@"blah" forControlState:UIControlStateNormal] . 使用[button setTitle:@"blah" forControlState:UIControlStateNormal]

This allows you to set a different title for highlighted, selected, up, etc. 这允许您为突出显示,选定,向上等设置不同的标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM