按下按钮后它的标题消失 [英] After push a button it's title disappears

查看:18
本文介绍了按下按钮后它的标题消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类似push-on-push-off的按钮,比如这个,虽然我的代码有点不同:

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

推荐答案

那是因为你的图片超过了你的标签.

That's because your image overalps your label.

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

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

这篇关于按下按钮后它的标题消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆