全球变化的UI颜色(斯威夫特)

问题描述:

我想要做这样的事情: 在设置应用程序,可以选择一种颜色,之后,像导航栏一样的UI元素,tabbar高亮将更改为该颜色。全球变化的UI颜色(斯威夫特)

这是否有啧啧的意思?

+3

你做了什么努力? –

+0

我可以通过在每个视图中设置导航栏颜色来做到这一点,但我想要的是在应用程序某处调用一个func并且它会全部更改 –

+0

您可以从链接获取一个创意http://stackoverflow.com/questions/31217748/uicolor代码在变量中快速 –

下面是你如何在Objective-C中做到这一点。

- (void)setCustomizedNavigationBarStyle { 


    // UIBarButtonItem styling 
    NSShadow *shadow = [[NSShadow alloc]init]; 
    shadow.shadowOffset = CGSizeMake(0.0, 1.0); 
    shadow.shadowColor = [UIColor clearColor]; 

    NSDictionary *enabledTextAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor darkGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:17.0]}; 

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal]; 

    NSDictionary *disabledTextAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:17.0]}; 

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled]; 



    // UINavigationBarTitle styling 
    NSDictionary *titleAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:18.0]}; 

    [[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[UINavigationController class]]]setTitleTextAttributes:titleAttributeDictionary]; 

} 

您可以在didFinishLaunchingWithOptions中打电话给我。一旦转化为斯威夫特,你会加入这一行didFinishLaunchingWithOptions

setCustomizedNavigationBarStyle() 

这应该是很容易翻译成斯威夫特。

此外,您可以创建一个自定义颜色调色板。您可能会发现在该主题有帮助的这篇文章:

How do I create a category in Xcode 6 or higher?

你可以保存颜色NSUserDefaults,每当你需要那种颜色设为您的视图元素通过键检索。你需要一个扩展名为NSUserDefaults,它返回一个UIColor。

查看this question的接受答案。

希望它有帮助!