Disabled buttons not changing image in

不打扰是莪最后的温柔 提交于 2019-12-11 11:09:38

问题


I have a button that pushes to a settings viewController. On that settings viewController I has a switch to invert all the colors on the original view, an inversion which I've done programmatically. I made inverted button images to replace the original images.

When I return to the original view, I have viewWillAppear call the method to invert if the switch has been flipped. Everything changes accordingly except for two disabled buttons.

The switch value is saved under the default settings so that the user can exit the app and come back later and still have the colors inverted. When viewDidLoad calls the invert method, the buttons change just fine, and they even show up adjusted to show they are disabled.

Any idea what might be going on?

-(void)invert{

self.view.backgroundColor = [UIColor blackColor];

//Buttons
[self.stopButton setImage:[UIImage imageNamed:@"stopinverted"]
                 forState:UIControlStateNormal];
[self.playButton setImage:[UIImage imageNamed:@"playinverted"]
                 forState:UIControlStateNormal];
}

-(void)viewWillAppear:(BOOL)animated{

    _inverted = 
      [[NSUserDefaults standardUserDefaults] boolForKey:@"isInvertedOn"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    if(_inverted){
        [self invert];
    } else {
        [self unInvert];
    }
    [super viewWillAppear:(BOOL)animated];
}

回答1:


The issue seems to be that the image is not updated automatically, you'll need to reset the enabled value to make it work:

[self.stopButton setImage:[UIImage imageNamed:@"stopinverted"]
                 forState:UIControlStateNormal];
self.stopButton.enabled = ! self.stopButton.enabled;
self.stopButton.enabled = ! self.stopButton.enabled;

[self.playButton setImage:[UIImage imageNamed:@"playinverted"]
                 forState:UIControlStateNormal];
self.playButton.enabled = ! self.playButton.enabled;
self.playButton.enabled = ! self.playButton.enabled;



回答2:


I think you are disabling stopButton and playButton before applying the image. And also you are applying UIImage for UIButton normal UIControlState. I would suggest do the following code changes

Change this

//Buttons
[self.stopButton setImage:[UIImage imageNamed:@"stopinverted"]
                 forState:UIControlStateNormal];
[self.playButton setImage:[UIImage imageNamed:@"playinverted"]
                 forState:UIControlStateNormal];

to

//Buttons
[self.stopButton setImage:[UIImage imageNamed:@"stopinverted"]
                 forState:UIControlStateDisabled];        
[self.playButton setImage:[UIImage imageNamed:@"playinverted"]
                 forState:UIControlStateDisabled];

In short you have to set UIButton UIImages in disabled state.

For more reference have a look this questions answer Xcode: set image on button only for default state, not also selected

If you set the UIImages according to the state of the UIButton then in the function in which you want to invert the UIImage, you have to only need to handle the state of the UIButton and it will automatically change the image.




回答3:


Thanks for the help. Here is the answer I came up with, inspired by A-Live's answer.

First I had to set the button as enabled, then set the image, and then disable the button again all within the same method. This has fixed my problem.

[self.playButton setEnabled:YES];
[self.playButton setImage:[UIImage imageNamed:@"playinverted"]forState:UIControlStateNormal];
[self.playButton setEnabled:NO];



回答4:


try using :

theButton.adjustsImageWhenDisabled = false

this will use your custom image instead of the default dimming.



来源:https://stackoverflow.com/questions/23609065/disabled-buttons-not-changing-image-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!