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];
}
I think you are disabling stopButton and playButton before applying the image. And also you are applying
UIImageforUIButtonnormalUIControlState. I would suggest do the following code changesChange this
to
In short you have to set
UIButtonUIImagesin 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
UIImagesaccording to the state of theUIButtonthen in the function in which you want to invert theUIImage, you have to only need to handle the state of theUIButtonand it will automatically change the image.