2017-10-06 19 views
0

私はUIStackViewにあるいくつかのボタンを持つプロジェクトを持っています。各ボタンにはbackgroundImageがあります。すべてが必要に応じてレンダリングされますが、半透明のbackgroundImageの前にグラデーションレイヤーを配置して、ボタンのテキストが少しコントラストを持つようにしたいと思います。UIButtonのbackgroundImageの上にグラデーションレイヤーを追加する

私は、各UIButtonのbackgroundImageの目の前に半透明のグラデーションを追加するviewDidLoadでこのコードを使用しようとしています:

buttonArray = [buttonOne, buttonTwo, buttonThree, buttonFour] 
let gradientLayer = CAGradientLayer() 
gradientLayer.colors = [UIColor(white: 1.0, alpha: 0.5)] 
// gradientLayer.colors = [UIColor(white: 1.0, alpha: 0.5).cgColor] 
// gradientLayer.colors = [UIColor.orange] 
// gradientLayer.colors = [UIColor.orange.cgColor] 
buttonArray.forEach({$0.imageView?.layer.insertSublayer(gradientLayer, at: 0)}) 

現状では、何も添加していないされます。私は、アルファ値なしで何かに色を変えてみたいし、希望の色を.cgColorに変更しようとしました。サイコロはありません。読んでくれてありがとう。 UIButtonのbackgroundImageの前にgradientLayerを配置する方法をお勧めします。

私は、コードは、私はあなたが複数の色だけでなく、層の境界を指定するために持っていると思うhere

+0

はあなたにそのレイヤーを追加してみましたこの画像を背景画像として割り当てますか? – antonio081014

+0

良いアイデア。私はそれを渦巻くよ – Adrian

答えて

0

た私の試みをオフに基づいて:

let buttonArray = [buttonOne, buttonTwo, buttonThree, buttonFour] 

    for button in buttonArray { 
     let gradientLayer = CAGradientLayer() 
     gradientLayer.colors = [UIColor(white: 0.5, alpha: 0.5).cgColor, UIColor(white: 1.0, alpha: 0.5).cgColor] 
     gradientLayer.frame = (button?.bounds)! 
     button?.layer.insertSublayer(gradientLayer, at: 0) 
    } 
+0

読んでいただきありがとうございます。それは私にとってはうまくいかなかった。 – Adrian

関連する問題