Hassy
Hassy

Reputation: 5208

Objective C - Altering appearance of control implementing appearance api

I am using the appearance api to customize UISearchBar

[[UISearchBar appearance] setSearchFieldBackgroundImage:searchBackground forState:UIControlStateNormal];
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setScopeBarBackgroundImage:backgroundImage];
[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];

I am using this in a class that is a parent to other classes so that the effect is implemented on all child classes.

Now I need to customize one of the subclass containing UISearchBar. I tried link

But I can't get that effect when the appearance is done on superclass. If I remove the appearance then I am able to have this effect.

How can I do this by implementing appearance api?

Upvotes: 1

Views: 265

Answers (2)

Hassy
Hassy

Reputation: 5208

I solved this using following code

[[UISearchBar appearance] setSearchFieldBackgroundImage:nil forState:UIControlStateNormal];
[[UISearchBar appearance] setBackgroundImage:nil];
[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];

for (id img in srchbar.subviews) {
    if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [img removeFromSuperview];
    }
}

Edit: Above solution can cause the others to display wrong. So the best solution is marked as right.

Upvotes: 0

Based on the Apple's documentation

    [[UISearchBar appearanceWhenContainedIn:[yourSubClassedViewControllerWhichHasSearchbar class], nil]
     setBackgroundColor:[UIColor orangeColor]];

Upvotes: 1

Related Questions