Question

Just having some hard time with UIButton. I remove all subviews using

for (UIView *v in button.subviews) {
    [v removeFromSuperview];
}

But I want to set the background image afterwards using

[button setBackgroundImage:[UIImage imageNamed:@"backgroundImage.png"] forState:UIControlStateNormal];

and nothing happens. If I do not remove all subviews than the previous code works.

So is it possivle that removing all subviews actually removes the backgroundImage as well, in which case, is it possible to put the backgroundImage back in?

Was it helpful?

Solution

Yes, according to what you experienced, the background image seems to be shown using a separate subview. Short answer: instead of raping poor view hierarchy (which is private anyway), you should keep track of the subviews you added yourself and remove only those.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top