2012-04-19 21 views
0

我想创建this one推在推客般的按钮,虽然我的代码有一点不同:推后一个按钮,它的标题消失

UIButton* alertsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
alertsButton.frame = CGRectMake(170, 43.5, 130, 130); 
[alertsButton.titleLabel setFont:[UIFont fontWithName:@"Novecento wide" size:22]]; 
[alertsButton setTitle:NSLocalizedString(@"Alerts", nil) forState:UIControlStateHighlighted]; 
[alertsButton setTitle:NSLocalizedString(@"Alerts", nil) forState:UIControlStateNormal]; 
[alertsButton setTitle:NSLocalizedString(@"Alerts", nil) forState:UIControlStateSelected]; 
[alertsButton addTarget:self action:@selector(toggleAlerts:)forControlEvents:UIControlEventTouchUpInside]; 

-(void)toggleAlerts:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 

    if (button.selected) { 
     button.selected = NO; 
     [button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal]; 
     [button.titleLabel setFont:[UIFont fontWithName:@"Novecento wide" size:22]]; 
     NSLog(button.titleLabel.text); 
    } else { 
     button.selected = YES; 
     [button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal]; 
     [button.titleLabel setFont:[UIFont fontWithName:@"Novecento wide" size:22]]; 
     NSLog(button.titleLabel.text); 
    } 
} 

当我按下按钮,背景会按预期更改,但标签会消失,尽管文本显示在调试控制台中。我试图this没有结果:(

回答

1

那是因为你的形象overalps标签。

而不是[button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];

[button setBackgroundImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];

0

您的开启和关闭图像什么尺寸?

如果他们太大,他们会将文本推出视图 正如尼基塔所说,如果图像应该是背景,请使用setBackgroundImage方法od