[英]Setting backgroundColor of custom UIButton class has no effect?
[英]UIButton custom backgroundColor in tvOS
我的TVos
應用程序內部有一個UIButton
,我想在其中更改backgroundColor
以及titleLabel的字體。
當按鈕變為focused view
時,text,font和backgroundColor
應該相同,但應顯示為focused view
。
我的ViewDidLoad
有以下三行代碼:
orderButton.titleLabel?.font = UIFont(name: "RobotoCondensed-Regular", size: 40)
orderButton.setBackgroundImage(getImageWithColor(UIColor(rgba: "#1db5b5")), forState: [.Focused, .Normal])
orderButton.setTitleColor(UIColor.whiteColor(), forState: [.Focused, .Normal])
問題
加載視圖時,按鈕仍具有默認的灰色。 當它聚焦時,它會獲得我在ViewDidLoad
內部設置的顏色。
有什么幫助嗎?
您將必須分別指定兩個狀態,像這樣
orderButton.setBackgroundImage(getImageWithColor(UIColor(rgba: "#1db5b5")), forState: .Focused)
orderButton.setBackgroundImage(getImageWithColor(UIColor(rgba: "#1db5b5")), forState: .Normal)
正如文檔所說
func setBackgroundImage(_ image: UIImage?,forState state: UIControlState)
我們一次只能指定一種狀態
您可以重寫didUpdateFocusInContext方法來實現此目的。 假設您的按鈕的標簽設置為1。現在您可以執行以下操作:
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if let nextFocusedView = context.nextFocusedView {
if button.tag == 1 {
// Change the font and background color here for Focused state
}
}
if let previousFocusView = context.previouslyFocusedView {
if button.tag == 1 {
// Change the font and background color here for Non-Focused state
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.