覆盖 Swift UIColor 类函数 [英] Override Swift UIColor Class Functions
问题描述
我正在尝试在 Swift 1.2 中为我的 iOS 应用程序实现暗"模式功能,并且正在考虑编写 UIColor 的扩展或子类.我想知道是否可以覆盖 UIColor.whiteColor()、UIColor.blackColor() 和其他内置颜色.如果没有,为整个应用程序实现暗模式的更好方法是什么?以下代码不能用作扩展.
I'm trying to implement a "dark" mode feature for my iOS application in Swift 1.2 and am thinking about writing an extension or subclass of UIColor. I was wondering if it's possible to override UIColor.whiteColor(), UIColor.blackColor(), and other built-in colors. If not, what's a better way to accomplish a dark mode for the entire app? The following code doesn't work as an extension.
class func whiteColor() -> UIColor {
return UIColor(...)
}
因为它与 UIColor 中现有的 whiteColor() 声明冲突.
because it conflicts with the existing declaration of whiteColor() in UIColor.
谢谢!
推荐答案
最好的解决方案是编写一个 ColorScheme 类来存储您的颜色(比如 labelColor、backgroundColor 等).当您将 ColorScheme 更改为暗模式"时,只需设置新颜色并推送 NSNotification.您的视图控制器应该监听它并将标签、背景等的颜色设置为新值.
The best solution would be to write a ColorScheme class that stores your colors (say labelColor, backgroundColor, ...). When you change your ColorScheme to "dark mode", just set the new colors and push an NSNotification. Your view controller(s) should listen to it and set the colors for labels, background, etc. to the new values.
这篇关于覆盖 Swift UIColor 类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!