2017-12-27 290 views
0

我已经阅读了很多关于如何自定义Views颜色的文章,但没有关于在iOS 11.x或之前版本中为导航栏,状态栏和标签栏等标准控件检索系统颜色。 UIColor类有3种系统颜色,但它们很无用。例如,调用UINavigationBar.appearance()几乎没有什么帮助,因为如果在应用程序中没有定义任何内容,它可能会返回默认灯光颜色方案的“清晰”颜色。那么为什么Apple不能像其他人那样通过编程方式获取系统颜色(对于Windows和Android)呢?有没有人知道在哪里可以找到它们?提前发送。iOS 11.x系统颜色

+0

您是否在寻找这些颜色? https://developer.apple.com/ios/human-interface-guidelines/visual-design/color/ – nathan

+0

系统的颜色是,但是通过编程方式,按名称,而不是RGB值,以便每次都获得正确的颜色他们被苹果改变的时间 – Sergiob

回答

1

iOS不提供以编程方式访问这些颜色的方法。

相反,它很简单,可以将这些颜色添加到您自己的项目中。如果未来iOS版本中的颜色发生变化(并且您想要使用这些新颜色),则需要更新您的应用。

在大多数情况下,这不是问题,因为应用程序为品牌目的定义自己的颜色。

enum SystemColor { 

    case red 
    case orange 
    case yellow 
    case green 
    case tealBlue 
    case blue 
    case purple 
    case pink 

    var uiColor: UIColor { 
     switch self { 
     case .red: 
      return UIColor(red: 255/255, green: 59/255, blue: 48/255, alpha: 1) 
     case .orange: 
      return UIColor(red: 255/255, green: 149/255, blue: 0/255, alpha: 1) 
     case .yellow: 
      return UIColor(red: 255/255, green: 204/255, blue: 0/255, alpha: 1) 
     case .green: 
      return UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) 
     case .tealBlue: 
      return UIColor(red: 90/255, green: 200/255, blue: 250/255, alpha: 1) 
     case .blue: 
      return UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1) 
     case .purple: 
      return UIColor(red: 88/255, green: 86/255, blue: 214/255, alpha: 1) 
     case .pink: 
      return UIColor(red: 255/255, green: 45/255, blue: 85/255, alpha: 1) 
     } 
    } 

} 

使用范例:

myView.backgroundColor = SystemColor.blue.uiColor 

如果你愿意的话,这些颜色也可以被定义为一个扩展上UIColor像这样:

extension UIColor { 
    static let systemBlue = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1) 
    // etc 
} 

和用法是这样的:

UIColor.systemBlue 

iOS system colors from the hig

Colors in the Human Interface Guidelines

+0

谢谢@nathan我知道几种获得这种结果的方法。我的应用没有自定义颜色。这取决于系统的颜色设计。所以,你的解决方案不能满足我的需求。无论如何感谢您的时间 – Sergiob

+0

@Sergiob没有解决方案可以满足您的需求,我知道。这是目前iOS API限制所能做的最好的。 – nathan