I have a UIButton with a different image for normal and selected state. Also I need to change the tint of the button depending on the app's theme.
But when I set the button to selected State to switch the image, it inverts its colours.
- (void)setLike:(BOOL)selected {
self.likeButton.selected = selected;
if (selected) {
self.likeButton.tintColor = [Theme getTintColor];
} else {
self.likeButton.tintColor = [Theme getLightColor];
}
}
Normal State
Actual Selected
Desired Selected
Note: I can't change the image because this code is used in another places in the app with different selected and unselected images.
- (void)setLike:(BOOL)selected {
if (selected) {
[self.likeButton setImage:[UIImage imageNamed:@"Liked"] forState:UIControlStateNormal];
self.likeButton.tintColor = [Theme getTintColor];
} else {
[self.likeButton setImage:[UIImage imageNamed:@"Like"] forState:UIControlStateNormal];
self.likeButton.tintColor = [Theme getLightBaseColor];
}
}
I would try to keep track of the state in a property and then have an additional properties for returning the correct image for the current state. You can then set these upon
viewDidLoad
orviewDidAppear
.I don't know your exact situation so here is an example.
Example: