Search code examples
iosswiftuibuttonswift2tvos

UIButton custom backgroundColor in tvOS


I have a UIButton inside my TVos app where I want to change the backgroundColor and also the font of the titleLabel.

The text,font and backgroundColor should be the same when the button gets focused but it should be shown that it becomes a focused view.

I have these three lines of code inside my 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])

PROBLEM

When my view is load, my button still has the default gray color. When it gets focused it gets the color that I've set inside my ViewDidLoad.

Any help on this?


Solution

  • you will have to specify both sates separately,like this

     orderButton.setBackgroundImage(getImageWithColor(UIColor(rgba: "#1db5b5")), forState: .Focused)
     orderButton.setBackgroundImage(getImageWithColor(UIColor(rgba: "#1db5b5")), forState: .Normal)
    

    As Docs say

      func setBackgroundImage(_ image: UIImage?,forState state: UIControlState)
    

    we have only one state at time to specify