I use addSubLayer in UIView and nothing happened

Hello! I am creating UIView (subview of main view) and I want to add gradient for background, but I don't use CGRect for create UIView only autolayout therefore my UIView has coordinates 0, 0, width 0 and hight 0. So when I try to use addSubLayer nothing happen. What should I do?

//gradient
var gradient: CAGradientLayer = {

        let gradient = CAGradientLayer()

        gradient.colors = [

            UIColor(red: 25/255, green: 25/255, blue: 186/255, alpha: 1).cgColor,

            UIColor(red: 177/255, green: 146/255, blue: 125/255, alpha: 1).cgColor

        ]

        gradient.startPoint = CGPoint(x: 0, y: 0.5)

        gradient.endPoint = CGPoint(x: 1, y: 0.5)

        return gradient

}()

//UIView
var win: UIView = {

    let view = UIView()

    view.translatesAutoresizingMaskIntoConstraints = false

    view.layer.addSublayer(gradient)

    gradient.frame = view.bounds

    return view

}()

//layout 
func winConstraint(_ collections: UICollectionView, _ view: UIView, _ btn: UIView) {

    btn.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 25).isActive = true

    btn.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true

    btn.topAnchor.constraint(equalTo: collections.bottomAnchor, constant: 8).isActive = true

    btn.heightAnchor.constraint(equalToConstant: 100).isActive = true

}

//from ViewController
view.addSubview(win)
Answered by NikitaDen in 699139022

So i found the solution! If we want to customize our button or view or something else view gradient background

  1. if we use only layout and don't use CGRect for button or UIView doesn't matter frame of our UIView is gonna be 0, 0, 0, 0

therefore

gradient.frame = (0, 0, 0, 0)

but its only in ViewDidLoad method:) if we will use viewDidLayoutSubviews() method, we will get updated size of our button or view and therefore we have to do

override func viewDidLayoutSubviews()
{
        super.viewDidLayoutSubviews...
        gradient.frame = view.bounds
}

Please show the complete code you have so far. To show what and where you do it.

Thanks for the feedback. But note that code should be pasted in Answer, not comments and formatted with code formatter (<>):

You do not show where you call winConstraint. In this code, what is the value of gradient.frame ?

gradient.frame = view.bounds

Where do you think you set it to its real value ?

// Gradient
var gradient: CAGradientLayer = {
    let gradient = CAGradientLayer()
    gradient.colors = [
        UIColor(red: 25/255, green: 25/255, blue: 186/255, alpha: 1).cgColor,
        UIColor(red: 177/255, green: 146/255, blue: 125/255, alpha: 1).cgColor
    ]
    gradient.startPoint = CGPoint(x: 0, y: 0.5)
    gradient.endPoint = CGPoint(x: 1, y: 0.5)
    return gradient 
}()

//UIVIew
var win: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.addSublayer(gradient)
    gradient.frame = view.bounds
    return view 
}()

func winConstraint(_ collections: UICollectionView, _ view: UIView, _ btn: UIView) {
    btn.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 25).isActive = true
    btn.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true
    btn.topAnchor.constraint(equalTo: collections.bottomAnchor, constant: 8).isActive = true
    btn.heightAnchor.constraint(equalToConstant: 100).isActive = true
}

// from ViewController class view.addSubview(win)

Not enough code to test.

But I would create the view with a (non zero) CGRect. As you set its size later, that will have no impact for UI.

Accepted Answer

So i found the solution! If we want to customize our button or view or something else view gradient background

  1. if we use only layout and don't use CGRect for button or UIView doesn't matter frame of our UIView is gonna be 0, 0, 0, 0

therefore

gradient.frame = (0, 0, 0, 0)

but its only in ViewDidLoad method:) if we will use viewDidLayoutSubviews() method, we will get updated size of our button or view and therefore we have to do

override func viewDidLayoutSubviews()
{
        super.viewDidLayoutSubviews...
        gradient.frame = view.bounds
}

Great. Effectively, in viewDidLoad a lot of objects are not initialised yet. For the future, when we ask to show full code, please show full code. No one can guess you did in viewDidLoad or elsewhere !

That was the purpose of my question:

To show what and where you do it.

I use addSubLayer in UIView and nothing happened
 
 
Q