【问题标题】:Change UI color globally(Swift)全局更改 UI 颜色(Swift)
【发布时间】:2015-10-14 01:20:09
【问题描述】:

我想做这样的事情: 在应用程序的设置中,可以选择一种颜色,之后一些UI元素,如导航栏,标签栏突出显示将变为该颜色。

有什么办法吗?

【问题讨论】:

  • 你做了哪些努力?
  • 我可以通过在每个视图中设置导航栏颜色来做到这一点,但我想要的是在应用程序的某个地方调用一个 func 并且它会全部更改
  • 你可以从链接stackoverflow.com/questions/31217748/…得到一个想法
  • 嗯?发生了什么?你搞清楚了吗?
  • 还没有。我的问题是我不能告诉元素改变颜色

标签: ios swift


【解决方案1】:

以下是您在 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?

【讨论】:

    【解决方案2】:

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

    查看this question 接受的答案。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 2014-09-25
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      相关资源
      最近更新 更多