gpt4 book ai didi

iOS UIButton 在将背景图像设置为 nil 后消失

转载 作者:行者123 更新时间:2023-11-29 03:46:26 25 4
gpt4 key购买 nike

我有一个没有标题或初始背景图像的 UIButton。按下时,按钮背景图像会发生变化,再次按下时,背景图像将设置为零。现在,在 iOS 6.x 中,该按钮完全消失了。这是我的代码:

- (IBAction)resetAll: (id) sender {
[self reset];

for (UIView *view in self.view.subviews) {

if ([[[view class] description] isEqualToString:@"UIRoundedRectButton"]) {
UIButton *theButton = (UIButton *)view;

int nTag = theButton.tag;

switch (nTag) {
case 11: {
theButton.tag = 10;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
break;
}
case 21: {
theButton.tag = 20;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 31: {
theButton.tag = 30;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 41: {
theButton.tag = 40;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 51: {
theButton.tag = 50;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
}
}
}
return;

这段代码工作正常:

- (IBAction)ButtonClicked: (id) sender {
UIButton *btn = (UIButton *)sender;

// Get the current tag value
int currentTag = [sender tag];

// Toggle the check buttons
if (currentTag == 10 || currentTag == 20 || currentTag == 30 || currentTag == 40 || currentTag == 50) {
[btn setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
btn.tag = ++currentTag;
}

else
if (currentTag == 11 || currentTag == 21 || currentTag == 31 || currentTag == 41 || currentTag == 51) {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
btn.tag = --currentTag;
}

[self calculate];

如有任何建议或帮助,我们将不胜感激!

最佳答案

与其在 UIControlStateNormal 中更改按钮的背景图像,为什么不直接更改按钮的状态呢?如果您以编程方式创建按钮,则只需添加以下行

[btn setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateSelected];

然后,当按下并且您希望它成为箭头图像时:

[btn setSelected:YES];

并将其设置回默认外观:

[btn setSelected:NO];

如果您在 XIB 中制作了按钮,则可以通过将状态配置更改为“选定”并在那里设置图像来设置状态图像。

关于iOS UIButton 在将背景图像设置为 nil 后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17706941/

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