iOS 交换颜色以更改应用程序的主题
iOS Swap Colors to Change Theme of App
我正在尝试为我的 iOS 应用实现色盲模式。现在,我定义了 3 个红色、绿色和蓝色的全局变量:
var red : UIColor = UIColor(...)
var green : UIColor = UIColor(...)
var blue : UIColor = UIColor(...)
现在,我有一个 UIButton,用户可以按下它来切换到色盲模式。一旦发生这种情况,我将这样更改全局变量:
red = newRed
green = newGreen
blue = newBlue
但是,这不会更新任何已加载的现有视图的颜色。有没有一种方法可以让我在不重新加载整个应用程序的情况下将所有红色控件更新为新的红色,将绿色控件更新为新绿色,将蓝色控件更新为新蓝色?谢谢!
更新到 post 我的按钮操作:
func changeColor(sender: AnyObject) {
red = UIColor(...) //this is different from the previous red
green = UIColor(...) //this is different from the previous green
blue = UIColor(...) //this is different from the previous blue
//if I call viewDidLoad() again, then the colors are changed for all objects. However, I don't think this is the correct thing to do.
}
切勿直接调用 viewDidLoad()
。您需要确保您的 IBOutlet
s 附加到视图控制器内的视图。此外,确保您直接更改视图的 backgroundColor
属性。例如:
myView.backgroundColor = UIColor.redColor()
我正在尝试为我的 iOS 应用实现色盲模式。现在,我定义了 3 个红色、绿色和蓝色的全局变量:
var red : UIColor = UIColor(...)
var green : UIColor = UIColor(...)
var blue : UIColor = UIColor(...)
现在,我有一个 UIButton,用户可以按下它来切换到色盲模式。一旦发生这种情况,我将这样更改全局变量:
red = newRed
green = newGreen
blue = newBlue
但是,这不会更新任何已加载的现有视图的颜色。有没有一种方法可以让我在不重新加载整个应用程序的情况下将所有红色控件更新为新的红色,将绿色控件更新为新绿色,将蓝色控件更新为新蓝色?谢谢!
更新到 post 我的按钮操作:
func changeColor(sender: AnyObject) {
red = UIColor(...) //this is different from the previous red
green = UIColor(...) //this is different from the previous green
blue = UIColor(...) //this is different from the previous blue
//if I call viewDidLoad() again, then the colors are changed for all objects. However, I don't think this is the correct thing to do.
}
切勿直接调用 viewDidLoad()
。您需要确保您的 IBOutlet
s 附加到视图控制器内的视图。此外,确保您直接更改视图的 backgroundColor
属性。例如:
myView.backgroundColor = UIColor.redColor()