0

I'm trying to create a push-on-push-off-like button like this one, although my code is a little different:

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);
    }
}

When I push the button, the background changes as expected but the label disappears although it's text is displayed in the debug console. I tried this without results :(

4

2 回答 2

1

那是因为你的形象超出了你的标签。

代替[button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];

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

于 2012-04-19T20:23:38.757 回答
0

您的开启和关闭图像尺寸是多少?

如果它们太大,它们会将文本推到视野之外。正如 Nikita 所说,如果图像应该是背景,请使用 setBackgroundImage 方法

于 2012-04-19T20:25:54.040 回答