0
我想实现添加主题到我创建的应用程序。只需切换2种颜色,我希望导航栏,工具栏等以选定的颜色显示。分离UIAppearence代码
当应用第一次加载时,我在AppDelegate的didFinishLaunchingWithOptions
方法中应用一种颜色。
UIColor *blueTheme = [UIColor colorWithRed:80/255.0f green:192/255.0f blue:224/255.0f alpha:1.0f];
UIColor *pinkTheme = [UIColor colorWithRed:225/255.0f green:87/255.0f blue:150/255.0f alpha:1.0f];
[[UINavigationBar appearance] setBarTintColor:pinkTheme];
[[UIToolbar appearance] setBarTintColor:pinkTheme];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
而在第一个视图控制器,我已经把分段控制切换颜色。
- (IBAction)themeChosen:(UISegmentedControl *)sender
{
if (sender.selectedSegmentIndex == 0) {
UIColor *blueTheme = [UIColor colorWithRed:80/255.0f green:192/255.0f blue:224/255.0f alpha:1.0f];
[[UINavigationBar appearance] setBarTintColor:blueTheme];
[[UIToolbar appearance] setBarTintColor:blueTheme];
} else {
UIColor *pinkTheme = [UIColor colorWithRed:225/255.0f green:87/255.0f blue:150/255.0f alpha:1.0f];
[[UINavigationBar appearance] setBarTintColor:pinkTheme];
[[UIToolbar appearance] setBarTintColor:pinkTheme];
}
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}
假设默认主题是粉色。我从分段控制切换到蓝色,并推送到下一个视图控制器,那里也有UIToolBar
。新选择的颜色(蓝色)仅适用于UIToolBar
,但不适用于UINavigationBar
。
有没有更好的方法来解决这个问题?此外,我想将与主题相关的代码放在单独的类中,因为它重复了很多代码。我怎么做?
谢谢。
我明白了。我现在知道了。谢谢 :) – Isuru