mergesort
mergesort

Reputation: 5187

Why is UIImageView on UIButton reappearing on touch?

I am setting the UIImageView on UIButton to an image, but hiding that image if something is loading. I am then adding a UIActivityIndicatorView to where the image view was. However, when I touch the button Title, the image view is suddenly appearing without changing the hidden property. It doesn't have anything to do with the selector/target I've added to the button because I've commented that out.

Button Creation:

[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:buttonTitles[i] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:buttonTitles[i]] forState:UIControlStateNormal];

Adding Indicator View in Button Category:

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[indicator startAnimating];
indicator.frame = self.imageView.frame;
[self addSubview:indicator];
self.imageView.hidden = YES;

Upvotes: 0

Views: 283

Answers (1)

Ahmed Mohammed
Ahmed Mohammed

Reputation: 1154

I think the best way to hide the image is to set nil for the UIControlStateNormal state instead. Replace

self.imageView.hidden = YES;

with

[self setImage:nil forState:UIControlStateNormal];

Upvotes: 1

Related Questions