UITabBar custom background image applied in one view controller instead of in the AppDelegate

StackOverflow https://stackoverflow.com/questions/22486023

Pergunta

I am working with themes in my iPhone app. I have two tabs in a tab bar (Timeline and Settings) and when the user goes to the second tab (Settings), selects themes in the settings and selects a theme, this styles the background image and the navigation bar of the first table view controller in the first tab (Timeline). This works.

I want to achieve the same thing with the tab bar, so the tab bar changes along with the background image and the navigation bar.

In my timeline table view, I put the following code in the viewWillAppear:

UIImage *tabBackgroundImage = [UIImage imageNamed:@"Purplepinktopp.png"];
[[UITabBar appearance] setBackgroundImage:tabBackgroundImage];

However, the tab bar never changes. The only way to change the tab bar is in the AppDelegate but that then changes it everywhere. I need to allow the user to select a theme and it changes accordingly and so I need to change this on a per view controller basis, exactly the same way with the navigation bar.

In the viewWillAppear of this timeline table view, I set the navigation bar with:

UIImage *navBackgroundImage = [UIImage imageNamed:@"Purplepinknav.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

That always works. This doesn't work for the Tab Bar however with the code above.

Edit: I've added the apperanceWhenContainedIn to the viewWillAppear:

UIImage *tabBackgroundImage = [UIImage imageNamed:@"SolidPurple.png"];
[[UITabBar appearance] setBackgroundImage:tabBackgroundImage];

[UITabBar appearanceWhenContainedIn:[UITabBar class], [UITabBar class], nil];

But that doesn't seem to be doing anything.

How can I go about fixing this? Any assistance would be immensely appreciated.

Foi útil?

Solução

There is a refinement to the UIAppearance proxy call you could try...

+ (id)appearanceWhenContainedIn:(Class <UIAppearanceContainer>)ContainerClass,...

This sets the appearance for a UIAppearance Proxy compliant object to the supplied values only when it's contained in a specific view controller subclass.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top