虽然我的代码有点不同,但我正在努力创建像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没有结果:(
答案 0 :(得分:1)
那是因为你的形象会影响你的标签。
而不是[button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];
写[button setBackgroundImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];
答案 1 :(得分:0)
你的开关图像尺寸是多少?
如果它们太大,它们会将文本推出视野。 正如Nikita所说,如果图像应该是背景,请使用setBackgroundImage方法