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

Posted

技术标签:

【中文标题】iOS UIButton在将背景图像设置为nil后消失【英文标题】:iOS UIButton disappears after setting background image to nil 【发布时间】:2013-07-17 18:00:32 【问题描述】:

我有一个没有标题或初始背景图像的 UIButton。按下时,按钮背景图像发生变化,再次按下时,它设置为 nil。现在在 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];

任何建议或帮助将不胜感激!

【问题讨论】:

【参考方案1】:

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

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

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

[btn setSelected:YES];

并将其设置回默认外观:

[btn setSelected:NO];

如果您在 XIB 中制作了按钮,则可以通过将状态配置更改为 Selected 并在此处设置图像来设置状态图像。

【讨论】:

感谢您的建议。我能够解决我的问题。这是实现我想要的更好的方法。【参考方案2】:

这是实现 DOC 答案的最终代码。我在界面生成器中设置了默认和选择状态:

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

if ([btn isSelected ]) 
    [btn setSelected:NO];
 else 
    [btn setSelected:YES];


[self calculate];



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

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

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

        [theButton setSelected:NO];

    

【讨论】:

【参考方案3】:

如果(像我一样)在按钮的选定状态周围已经有大量逻辑(父类基于此为视图提供状态),请提供一个额外的答案。如果您需要按钮以带有图像的未选中状态开始并使用无图像将状态更改为选中状态,那么您可以像这样将生成的空白图像添加到选中状态。

            //Make a stupid blank image because setting to nil on a state doesn't work.
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(23, 23), NO, 0.0);
            UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            UIImage *buttonNonSelectedImage = [UIImage imageNamed:@"video-slomo"];
            [self.myButton setBackgroundImage:buttonNonSelectedImage forState:UIControlStateNormal];
            [self.mybutton setBackgroundImage:blank forState:UIControlStateSelected];
            [self.myButton addTarget:self
                                    action:@selector(onMyButtonPress)
                          forControlEvents:UIControlEventTouchUpInside];
            self.myButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
            [self.playRateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [self.view addSubview:self.myButton];

然后,在您的按钮操作中,您可以更改状态(在我的情况下添加文本)。

- (void)onMyButtonPress

    if (self.myButton.isSelected) 
        [self.myButton setSelected:NO];
    
    else 
        [self.myButton setTitle:@"Selected"]
                              forState:UIControlStateSelected];
        [self.myButton setSelected:YES];
     

接受的答案在大多数情况下都有效,我只需要选择状态来获取其他跟踪信息。快乐编程。

【讨论】:

以上是关于iOS UIButton在将背景图像设置为nil后消失的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发中如何设置UIButton的Aspect Fit背景图片

在 Swift 中设置 UIButton 图像 - 当前解包为 nil

UIButton:设置图像视图的背景(不设置图像)

为uibutton背景图像iphone sdk设置渐变颜色(颜色)

UIButton:为选中的高亮 UI 状态设置背景颜色(不是图像)

在 Interface Builder (XCode 4) 中设置 UIButton 拉伸背景图像