Tae
Tae

Reputation: 1695

After push a button it's title disappears

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 :(

Upvotes: 0

Views: 1470

Answers (2)

Lefteris
Lefteris

Reputation: 14677

What size is your on and off image?

If they are too large they will push the text out of view. As Nikita said, if the image is supposed to be a background, use the setBackgroundImage method

Upvotes: 0

Nikita Pestrov
Nikita Pestrov

Reputation: 5966

That's because your image overalps your label.

Instead of [button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];

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

Upvotes: 1

Related Questions