使用新的背景,导航栏图像和标签栏图像为我的iPhone应用程序添加主题

问题描述 投票:0回答:1

我正在使用我的应用程序,让用户可以选择在应用程序设置中选择和切换主题。

我有一个三标签的标签栏控制器。

标签1 =时间表;选项卡2 =事件和选项卡3 =所有三个都是表视图的设置。

用户转到“设置”,单击“主题”单元格并选择主题。通过NSUserDefaults,我已经根据主题改变了背景图像和导航栏。但是,我正在努力让Tab Bar图像一致地改变。

现在,它已应用于AppDelegate,但它适用于每个场景。

在我的时间轴TableView和viewWillAppear中,我有:

else if ([self.selectedTheme isEqualToString:@"Twirl"])
{
    ThemeManager *themeManager = [ThemeManager sharedManager];
    themeManager.backgrounds = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ReddishBlack.png"]];
    self.tableView.backgroundView = themeManager.backgrounds;
    UIImage *navBackgroundImage = [UIImage imageNamed:@"ReddishBlackNav.png"];

    [[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

}

这非常有效。

但是,如果在上面的if语句结尾处,我添加:

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

当我在设置中选择此主题时,标签栏永远不会更改。

我在网上搜索了一下,发现了这个:

UIImage *tabBackground = [[UIImage imageNamed:@"tabBarBackground.jpg"] 
                          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set background for all UITabBars
[[UITabBar appearance] setBackgroundImage:tabBackground];
// Set background for only this UITabBar
[[tabBarController tabBar] setBackgroundImage:tabBackground];

我想为整个应用设置此项。因此,如果用户点击“绿色”主题,则绿色标签栏将应用于整个应用。我怎样才能做到这一点?

另外,如果我必须设置只有这个UITabBar的背景,如上面的代码所示,我必须为每个类做这个,我很高兴,但我如何得到[[tabBarController tabBar]的引用?

对此的任何帮助都会非常有帮助。我基本上希望确保Tab栏主题在选择时保持与背景和导航栏相同的主题。

ios uitabbarcontroller uitabbar uiappearance
1个回答
0
投票

这已在我的viewWillAppear中使用以下代码修复:

    UIImage *tabBackground = [[UIImage imageNamed:@"purplytab.png"]
                              resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self.tabBarController.tabBar setBackgroundImage:tabBackground];
© www.soinside.com 2019 - 2024. All rights reserved.