2016-07-16 8 views
0

プログラムでUIViewにグラデーションを追加しようとしていますが、動作しません。色が全くないように見えます。関連するコードとスクリーンショットを添付しました。私がグラデーションを適用している下の正方形に注目してください。誰かが私がここで間違っていることを理解するのを助けることができますか?UIViewにグラデーションを適用しないでください

Screenshot

let sundayView = UIView() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    setupViews() 
    setupSundayView() 
} 

func setupViews() { 
    sundayView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(sundayView) 
} 

func setupSundayView() { 
    sundayView.translatesAutoresizingMaskIntoConstraints = false 

    NSLayoutConstraint.activateConstraints([ 
     sundayView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor), 
     sundayView.topAnchor.constraintEqualToAnchor(fridayView.bottomAnchor, constant: 16.0), 
     sundayView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor, constant: -8.0),    
     sundayView.heightAnchor.constraintEqualToAnchor(mondayView.heightAnchor), 
     sundayView.widthAnchor.constraintEqualToAnchor(mondayView.widthAnchor) 
     ]) 

    let gradient = CAGradientLayer() 
    gradient.frame = sundayView.bounds 
    gradient.colors = [ 
     UIColor(red:1.00, green:0.37, blue:0.23, alpha:1.0).CGColor, 
     UIColor(red:1.00, green:0.16, blue:0.41, alpha:1.0).CGColor 
    ] 
    sundayView.layer.insertSublayer(gradient, atIndex: 0) 
} 
+0

はあなたではないでした'QuartzCore'フレームワークをインポートするのを忘れましたか? –

+0

私はその必要はないと思います。私はスーパービューにグラデーションを適用しようとしました。それはインポートすることなく働いた。 –

答えて

1

sundayViewためboundsができなくなりますので、あなたのグラデーションが表示されていませんまで自動レイアウトが実行されます。 viewDidLayoutSubviewsを上書きし、そこでグラデーションフレームを設定します。

あなたのViewControllerにこのプロパティを追加します。setupSundayView

var sundayGradient: CAGradientLayer? 

プロパティにgradientを保存:

let gradient = CAGradientLayer() 
sundayGradient = gradient 

その後はviewDidLayoutSubviewsにフレームを設定します。

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    sundayGradient?.frame = sundayView.bounds 
} 
+0

うーん..すみません、理解できません。 'let gradine = CAGradientLayer()'の後の 'setupSundayView'の –

+1

を' sundayGradient = gradient'という行に追加してください。 – vacawama

+0

ありがとう!それは働いた:)しかし、それは私が追加したコードの別の行を破っている: 'sundayView.layer.cornerRadius = 5'。 –

0

あなたがこのようにそれを試して、勾配層にlocationsを追加するのを忘れているようです:

func setupSundayView() { 
    sundayView.translatesAutoresizingMaskIntoConstraints = false 

    NSLayoutConstraint.activateConstraints([ 
     sundayView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor), 
     sundayView.topAnchor.constraintEqualToAnchor(fridayView.bottomAnchor, constant: 16.0), 
     sundayView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor, constant: -8.0),    
     sundayView.heightAnchor.constraintEqualToAnchor(mondayView.heightAnchor), 
     sundayView.widthAnchor.constraintEqualToAnchor(mondayView.widthAnchor) 
     ]) 

    let gradient = CAGradientLayer() 
    gradient.frame = sundayView.bounds 
    gradient.colors = [ 
     UIColor(red:1.00, green:0.37, blue:0.23, alpha:1.0).CGColor, 
     UIColor(red:1.00, green:0.16, blue:0.41, alpha:1.0).CGColor 
    ] 
    gradient.locations = [0.0, 0.5] 
    sundayView.layer.insertSublayer(gradient, atIndex: 0) 
} 
+0

まだ動作しません。 : –

+0

あなたの 'viewDidLoad'では' setupViews() 'の前に' setupSundayView() '関数を呼び出そうとしています –

+0

sundayViewフレームですsundayView.frame.size = CGSize(width:130、height: 13030) それは働いた –

関連する問題