Get size of subviews or/and UI elements through code

I have an App which works with stack Views, to create a good looking UI. To make the App even prettier I want to add color gradient for table view cells and other elements which have a fixed size, this is not a problem. For subviews which change the size depending on the device and orientation it is.
Code Block let topLayer = CAGradientLayer()
        topLayer.frame = CGRect(x: 0, y: 0, width: 9000, height: 70)
        topLayer.colors = [UIColor(red: 0.13, green: 0.22, blue: 0.35, alpha: 0.80).cgColor , UIColor(red: 0.18, green: 0.26, blue: 0.39, alpha: 1.00).cgColor]
        topLayer.locations = [0.0, 1.0]
        topView.layer.insertSublayer(topLayer, at: 0)

This is how I create the gradient color. How can get the CGRect Values of a subview or is there another way to gradient color without using the frame of CAGradientLayer ?

Answered by OOPer in 673129022
Have you tried doing it in viewDidLayoutSubviews()?
Code Block
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//...
}



By the way, your code does not seem to have anything to do with SwiftUI. You should use the tag UIKit instead of SwiftUI.

How can get the CGRect Values of a subview

I probably miss something.

If you get the subView.frame, then you'll have its size, isn't it ?
Yes that should happen, but if I set the topLayer.frame = topView.frame the layer is not shown correctly. Depending which device I choose in the simulator the layer is either shown too big or too small. I am looking for a solution, where the layer is always as big as the view. In my example the topView is a view where a button and a caption is placed. Depending on the device the height and width are changing, so I want that the size of the layer is changing too. I hope my problem is understandable.
No, not very clear.

Should show full code instead.
Code Block
// Outlets
@IBOutlet weak var topView: UIStackView!
//view did load
 override func viewDidLoad() {
        super.viewDidLoad()
let topLayer = CAGradientLayer()
        topLayer.frame = topView.frame
        topLayer.colors = [UIColor(red: 0.13, green: 0.22, blue: 0.35, alpha: 0.80).cgColor , UIColor(red: 0.18, green: 0.26, blue: 0.39, alpha: 1.00).cgColor]
        topLayer.locations = [0.0, 1.0]
        topView.layer.insertSublayer(topLayer, at: 0)
}

If I use this code and run it on the simulator the layer is not as big as the view. Unfortunately I can not attach a picture to show you how it looks. I want the layer always as big as the view.
Maybe it is clearer now ?

Accepted Answer
Have you tried doing it in viewDidLayoutSubviews()?
Code Block
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//...
}



By the way, your code does not seem to have anything to do with SwiftUI. You should use the tag UIKit instead of SwiftUI.
Thank you, it worked and you are right, I am using UIKit, sorry for that mistake
Get size of subviews or/and UI elements through code
 
 
Q