简体   繁体   English

UITabBar自定义背景图像应用在一个视图控制器中,而不是应用在AppDelegate中

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

I am working with themes in my iPhone app. 我正在我的iPhone应用程序中处理主题。 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: 在时间轴表视图中,将以下代码放入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. 更改选项卡栏的唯一方法是在AppDelegate中,然后随处更改它。 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: 在此时间轴表视图的viewWillAppear中,我将导航栏设置为:

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: 编辑:我已经将apperanceWhenContainedIn添加到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. 任何帮助将不胜感激。

There is a refinement to the UIAppearance proxy call you could try... 您可以尝试对UIAppearance代理调用进行改进。

+ (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. 仅当特定的视图控制器子类中包含UIAppearance Proxy兼容对象的外观时,才将其外观设置为提供的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM