Search code examples
iosswiftautolayout

width not picked up by cgrect()


i wanted to ask why doesnt my layer.frame = cgrect() doest not pick up the width (350) which i have set in the viewDidLayoutSubviews even though it does get printed in the console. but the width remains the same(390) as i have set in the storyboard.

override func viewDidLayoutSubviews() {

        width = masjidName.frame.width

        print(width)


    }


    override func viewDidLoad() {
        super.viewDidLoad()

        viewDidLayoutSubviews()

        let bottomLine1 = CALayer()
        bottomLine1.frame = CGRect(x: 0.0, y: masjidName.frame.height - 1, width: width, height: 1)
        bottomLine1.backgroundColor = UIColor.black.cgColor

        let bottomLine2 = CALayer()
        bottomLine2.frame = CGRect(x: 0.0, y: masjidName.frame.height - 1, width: width, height: 1)
        bottomLine2.backgroundColor = UIColor.black.cgColor

}

Solution

  • I got it to work by moving what I had In viewdidload into viewdidappear. As it takes a while for widths to update and viewdidload is called before it.