gpt4 book ai didi

ios - 按下按钮后标题消失

转载 作者:行者123 更新时间:2023-11-29 04:42:10 25 4
gpt4 key购买 nike

我正在尝试创建一个类似“按下-按下-关闭”的按钮,例如 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没有结果:(

最佳答案

那是因为您的图像超出了您的标签。

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

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

关于ios - 按下按钮后标题消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10236092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com