全局更改UI颜色(Swift)

时间:2015-10-14 01:20:10

标签: ios swift

我想做这样的事情: 在设置app时,可以选择一种颜色,之后有一些UI元素如Navigationbar,tabbar高亮显示会变为那种颜色。

对此有什么好感吗?

2 个答案:

答案 0 :(得分:0)

以下是您在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中调用它。转换为Swift后,您需要将此行添加到didFinishLaunchingWithOptions

setCustomizedNavigationBarStyle()

这应该很容易翻译成Swift。

除此之外,您还可以创建自定义颜色调色板。您可以在有用的主题上找到这篇文章:

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

答案 1 :(得分:0)

您可以将颜色保存在NSUserDefaults中,并在需要将该颜色设置为视图元素时通过键检索颜色。您需要NSUserDefaults的扩展名才能返回UIColor。

查看已接受的this question答案。

希望它有所帮助!

相关问题