2017-05-22 4 views
0

setTitleColor(.white, for: .normal)ボタンを白に設定することで、ボタンの色を設定できます。しかし、もしボタン内のタイトルの色をチェックしたいのであれば?uibutton内のタイトルの色を調べる

私は let color = titleColor(for: .normal)let color = titleLabel?.textColorを試しましたが、その色を別の場所に設定しようとしても何も起こりません。

使用:

extension UIButton { 

func loadingIndicator(show: Bool) { 
    let tag = 9876 

    var color: UIColor? 

    if show { 
     color = titleColor(for: .normal) 
     let indicator = UIActivityIndicatorView() 
     let buttonHeight = self.bounds.size.height 
     let buttonWidth = self.bounds.size.width 
     indicator.center = CGPoint(x: buttonWidth/2, y: buttonHeight/2) 
     indicator.tag = tag 
     indicator.color = UIColor.white 
     setTitleColor(.clear, for: .normal) 

     self.addSubview(indicator) 
     indicator.startAnimating() 
    } else { 
     if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView { 
      indicator.stopAnimating() 
      indicator.removeFromSuperview() 
      setTitleColor(color, for: .normal) 
     } 
    } 
} 
} 
+0

? –

答えて

0

これは、あなたがstateUIButtonのタイトルの色を取得することができますし、あなたがそれを使用することができますよりも、どこでもあなたが望む方法です。

let color = btn.titleColor(for: .normal); 
label.textColor = color 

私のボタンの色は、このイメージのLabel TextColorで正しく使用されています。 enter image description here

これは、あなたが拡張

extension UIButton { 

    func loadingIndicator(show: Bool) { 
     let tag = 9876 

     var color: UIColor? 

     if show { 
      color = titleColor(for: .normal) 
      let indicator = UIActivityIndicatorView() 
      let buttonHeight = self.bounds.size.height 
      let buttonWidth = self.bounds.size.width 
      indicator.center = CGPoint(x: buttonWidth/2, y: buttonHeight/2) 
      indicator.tag = tag 
      indicator.color = UIColor.white 
      setTitleColor(.clear, for: .normal) 
      self.addSubview(indicator) 
      indicator.startAnimating() 
     } else { 
      if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView { 
       indicator.stopAnimating() 
       indicator.removeFromSuperview() 
       setTitleColor(color, for: .normal) 
      } 

     } 
    } 
} 

でそれを行うと、そのようにそれを使用する方法である:あなたが他の場所でのタイトルの色を使用しているか

//This will set the saved color for `UIButton` title 
btn.loadingIndicator(show: false) 
+0

hmm。私はエクステンションの色を見つけようとしています。これは私が色を保存し、色をクリアして元の色に戻すことができるようにするためです。私は何をしようとしているのかを表示するために拡張子を追加しました – luke

+0

okを確認させてください –

+0

let indicator = self.viewWithTag(tag)を次のようにすれば、この文はfalseになります。 UIActivityIndi​​catorView私がfalse値でloadIndicatorを呼び出すとき。また、あなたはこの文をsetTitleColor(color:for::normal)のif文の外で実行する必要があります。 –

関連する問題