我有一个主题 class ,有几个领域。
背景颜色,字体颜色和元素颜色。
此类也几乎没有静态变化
class Themes {
static let defaultTheme = Themes(topColor: "5856D6",bottomColor: "C644FC",elementColor: "1F1F21",fontColor: "2B2B2B", name:"one")
static let defaultTheme1 = Themes(topColor: "5AD427",bottomColor: "FFDB4C",elementColor: "1F1F21",fontColor: "2B2B2B", name:"two")
static let defaultTheme2 = Themes(topColor: "FB2B69",bottomColor: "FF5B37",elementColor: "1F1F21",fontColor: "2B2B2B", name:"three")
static let defaultTheme3 = Themes(topColor: "52EDC7",bottomColor: "5AC8FB",elementColor: "1F1F21",fontColor: "2B2B2B", name:"four")
static let defaultTheme4 = Themes(topColor: "5AD427",bottomColor: "FFDB4C",elementColor: "1F1F21",fontColor: "2B2B2B", name:"five")
let topColor:UIColor
let bottomColor:UIColor
let elementColor:UIColor
let fontColor:UIColor
}
我的视图控制器默认加载第一个。
override func viewDidLoad() {
super.viewDidLoad()
self.initView(ThemeManagement.sharedInstance.getTheme())
}
我有一个按钮,它可以通过更改默认值之一的
next
并在defaultTheme
之后循环回到defaultTheme4
来更改主题最初在initView内部运行的是正确设置视图。但是每次通过都不会更新视图。尽管正在打印该功能的日志消息。
func initView(objectColor : Themes){
println("initing theme "+objectColor.name+"\n"+objectColor.topColor.debugDescription)
let background = CAGradientLayer().gradient(objectColor.topColor, bottomColorCode: objectColor.bottomColor)
background.frame = self.view.bounds
self.view.layer.insertSublayer(background, atIndex: 0)
codes.delegate = self
codes.placeholder = "####"
codes.textAlignment = NSTextAlignment.Center
codes.font = Themes.defaultThemeWithFontSize(.H2)
codes.textColor = objectColor.fontColor
segments.tintColor = objectColor.elementColor
segments.setTitle(String.fontAwesomeIconWithName(.Male), forSegmentAtIndex: 0)
segments.setTitle(String.fontAwesomeIconWithName(.VenusMars), forSegmentAtIndex: 1)
segments.setTitle(String.fontAwesomeIconWithName(.Female), forSegmentAtIndex: 2)
var font = UIFont.fontAwesomeOfSize(fontsize/2);
var attr = NSDictionary(object: font, forKey: NSFontAttributeName)
segments.setTitleTextAttributes(attr as [NSObject : AnyObject], forState:UIControlState.Normal)
acceptButton.titleLabel?.font = UIFont.fontAwesomeOfSize(fontsize)
acceptButton.setTitle(String.fontAwesomeIconWithName(FontAwesome.ArrowCircleORight), forState: .Normal)
acceptButton.tintColor = UIColor.greenColor()
workedButton.setTitle("yes", forState: UIControlState.Normal)
workedButton.tintColor = objectColor.fontColor
workedButton.layer.cornerRadius = 5
workedButton.titleLabel?.font = Themes.defaultThemeWithFontSize(.H2)
workedButton.backgroundColor = UIColor(red:0.333, green:0.937, blue:0.796, alpha: 0.2)
didntWorkButton.setTitle("no", forState: UIControlState.Normal)
didntWorkButton.tintColor = objectColor.fontColor
didntWorkButton.layer.cornerRadius = 5
didntWorkButton.titleLabel?.font = Themes.defaultThemeWithFontSize(.H2)
didntWorkButton.backgroundColor = UIColor(red:0.333, green:0.937, blue:0.796, alpha: 0.2)
locationButton.setTitle("Current Location", forState: UIControlState.Normal)
locationButton.tintColor = objectColor.fontColor
locationButton.layer.cornerRadius = 5
locationButton.titleLabel?.font = Themes.defaultThemeWithFontSize(.H3)
didItWork.text = "Did it work?"
didItWork.font = Themes.defaultThemeWithFontSize(.H2)
didItWork.textColor = objectColor.fontColor
self.view.setNeedsDisplay()
}
我是否需要做其他事情来更新背景
layer
和更改字体颜色? 最佳答案
这样
self.view.layer.insertSublayer(background, atIndex: 0)
每次您在旧的后面插入新的背景层,这样您就不会看到它。
并更改视图的tintColor不会立即更改视图的颜色,因此请尝试直接更改它的textColor
由cripto编辑
上面的答案是正确的。我只是想添加我的解决方案。
我只是保留对该层的引用并删除它,只要它不为null。这保证了我可以在第一遍设置它,然后在以后每次更改它。
if((backgroundLayer) != nil){
backgroundLayer.removeFromSuperlayer()
}
backgroundLayer = CAGradientLayer().gradient(objectColor.topColor, bottomColorCode: objectColor.bottomColor)
backgroundLayer.frame = self.view.bounds
self.view.layer.insertSublayer(backgroundLayer, atIndex: 0)
关于ios - 快速重绘 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31017539/