-1

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

}
DonMag
  • 69,424
  • 5
  • 50
  • 86
salman
  • 27
  • 6
  • remove all constraints from the storyboard if you have any – Julian Silvestri Apr 09 '20 at 17:04
  • that would ruin my design wouldnt it? – salman Apr 09 '20 at 17:36
  • First, never, never, never call `viewDidLayoutSubviews()`. If you comment-out your entire `viewDidLoad()` function, but leave `viewDidLayoutSubviews()` as you have it, what value do you get from from your current `print(width)` statement? – DonMag Apr 10 '20 at 20:12

2 Answers2

0

Move your entire code to viewDidLayoutSubviews. viewDidLayoutSubviews is a callback method and you should not call this explicitly. I would suggest you to go through ViewController lifecycle HERE

Mohammad Sadiq
  • 5,070
  • 28
  • 29
0

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.

salman
  • 27
  • 6