按钮setTitle无法工作/不可见

时间:2012-07-17 10:32:40

标签: objective-c button

我的按钮有一个简单的问题。标题不可见。我的代码是:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"ikonki%d.png",i]] forState:UIControlStateNormal];
    [button setTitle:@"Normal" forState:UIControlStateNormal];
    [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
    [button setEnabled:YES];

    [button addTarget:self
               action:@selector(buttonHandler:) 
     forControlEvents:UIControlEventTouchUpInside];


    button.tag = i;
    button.frame = CGRectMake(10. + 77. * i, 125., 68 , 68);


    [self.view addSubview:button];

2 个答案:

答案 0 :(得分:6)

是的,当您使用image属性时,这是正常的行为。

使用 backgroundImage 属性而不是 image 属性,如下所示:

[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ikonki%d.png",i]] forState:UIControlStateNormal];

更新#1:

重新定位按钮下按钮的标题,使用这种方式:

[button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, -60.f, 0.f)]; // align the values for your specific button
[button setClipsToBounds:false];

更新#2:

更改标题的字体,试试这个:

[button.titleLabel setFont:[UIFont systemFontOfSize:34.f]]; // set the font size what you desire

答案 1 :(得分:0)

如果您忘记使用按钮上的@synthesize或与之相关的变量,也会发生这种情况。