2016-01-04 30 views
0

我在TVos應用程序中有一個UIButton應用程序,我想要更改backgroundColor以及titleLabel的字體。UIButton在tvOS中的自定義backgroundColor

當按鈕變爲焦點時,文本,字體和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中設置的顏色。

對此有何幫助?

回答

2

你將不得不單獨指定兩個張祖興,這樣

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

由於文檔說

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

我們在一次只能有一個狀態到指定

+0

它的伎倆。但是,你不能以任何方式在一條線上做到這一點嗎? – Steaphann

+0

據商務部說 FUNC了setBackgroundImage(_圖像:UIImage的? forState狀態:UIControlState) 我們在一次只能有一個狀態來指定, –

+0

如果它的伎倆,請接受答案:) –

0

可以覆蓋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 
     } 
}