[英]Swift 4 - setNeedsDisplay and layoutIfNeeded not redrawing UILabel on self.UIView
基本上,我有一個自定義UILabel子類,該子類可基於變量設置標簽顏色。
class MyLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
textColor = GlobalVars.mainTextColor
}
}
在我的視圖控制器上,我有一個設置白色背景並將文本顏色變量設置為黑色的按鈕:
@IBAction func btnWhite(_ sender: UIButton) {
GlobalVars.mainTextColor = UIColor.black
GlobalVars.bgColor = UIColor.white
self.view.backgroundColor = UIColor.white
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
DispatchQueue.main.async { [weak self] in
self?.view.setNeedsLayout()
self?.view.layoutIfNeeded()
}
}
單擊此按鈕並更新變量后,我希望ViewController重繪標簽,這將更新其文本顏色。 如果我彈出VC並返回,則可以正常工作,但是我希望在單擊按鈕時更新顯示。 我在視圖上有一堆標簽,並且所有標簽都設置為myLabel類。 我不需要手動編碼更改到屏幕上的每個標簽,我只想重繪它。 UIView不是自定義類,而只是View Controller附帶的默認UIView。 這應該已經在主線程上發生了,但是我已經嘗試添加dispatch.main.async,以防萬一。 我希望最后都不需要。 我的視圖控制器布局的圖像在這里
當我單擊該按鈕時,背景變為白色,但是標簽文本的顏色沒有更新,我認為這是因為它沒有重繪它們。 還有第二個按鈕btnBlack,可以將其切換為與明暗模式效果完全相反的按鈕。
有什么想法嗎?
您必須在按鈕單擊方法中更改TextColor
@IBAction func btnWhite(_ sender: UIButton) {
myCustomLabel.textColor = UIColor.blue
}
無需調用以下提到的方法
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
注意:沒有其他方法可以更改UIlabel TextColor
每當GlobalVars.mainTextColor
或GlobalVars.bgColor
值更改時,您都必須再次設置所有標簽的顏色。
一種選擇是使用通知。 您可以在didSet
的全局變量上發布通知,並在MyLabel
類中捕獲該通知並更新顏色。
class GlobalVars {
static let onChangeColorNotification = "onChangeColorNotification"
static var mainTextColor:UIColor? {
didSet {
NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: onChangeColorNotification)))
}
}
}
然后在您的MyLabel
課堂中
class MyLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
onChangeGlogabColor()
NotificationCenter.default.addObserver(self, selector: #selector(onChangeGlogabColor), name: NSNotification.Name(rawValue: GlobalVars.onChangeColorNotification), object: nil)
}
@objc func onChangeGlogabColor() {
textColor = GlobalVars.mainTextColor
}
}
您可以對所有自定義類(如MyButton
或MyView
執行相同的操作,以捕獲通知並更新顏色。
而且您不必調用setNeedsDisplay()
或layoutIfNeeded()
。
@IBAction func btnWhite(_ sender: UIButton) {
GlobalVars.mainTextColor = UIColor.black
GlobalVars.bgColor = UIColor.white
self.view.backgroundColor = UIColor.white
}
比拉勒的答案是正確的。 有時需要一種完全不同的方法。 感謝您的關注,這里是最終代碼。
在下面添加代碼,並將所有標簽設置為此“ MyLabel”類。 然后,只要更改GlobalVars.mainTextColor變量,標簽就會更新其文本顏色。 它可以適用於UIButton或變量更改時實際上需要更新的任何屬性。 這將在已加載的任何VC上使用此類更新所有標簽。 這也解決了我的VC根標簽也需要更新的問題,因為它們也已經用原始顏色繪制,非常漂亮!
class MyLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
textColor = GlobalVars.mainTextColor
NotificationCenter.default.addObserver(self, selector: #selector(onChangeGlobalColor), name: NSNotification.Name(rawValue: "onChangeColorNotification"), object: nil)
}
@objc func onChangeGlobalColor() {
textColor = GlobalVars.mainTextColor
}
}
struct GlobalVars {
static var mainTextColor:UIColor = UIColor() {
didSet {
NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: "onChangeColorNotification")))
}
}
@IBAction func btnWhite(_ sender: UIButton) {
GlobalVars.mainTextColor = UIColor.black
self.view.backgroundColor = UIColor.white
}
@IBAction func btnBlack(_ sender: UIButton) {
GlobalVars.mainTextColor = UIColor.white
self.view.backgroundColor = UIColor.black
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.