8

It's driving me crazy this. Only on first run the viewDidLayoutSubviews is called twice.

Here is the code I'm using:

class CICViewController: UIViewController {


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



    func addQLabel(qLabel: UILabel, questionString: String, bgrLabel: UILabel) {// some code goes here
    }

    func makeRoundQButtons(qButtons:[UIButton]) {
      // some code goes here

    }

    func addArrows(numberOfArrows:Int, buttonCurAngle:Double) {//some code goes here
    }

    func animateButtons(qButtons:[UIButton], buttonCurAngle:Double) {

     // some code goes here

    }



    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

    }

    func backTapped(sender: UIBarButtonItem) {
        navigationController?.popViewControllerAnimated(false)
       //some code goes here

    }

    func restartTapped(sender: UIBarButtonItem) {
        navigationController?.popToRootViewControllerAnimated(false)
        //some code goes here
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

} 

And in my ViewController I call this :

class OneViewController: CICViewController {

  override func viewDidLoad() {
        super.viewDidLoad()

 //some code goes here
}


    override func viewDidLayoutSubviews() {

        super.viewDidLayoutSubviews()
        print("viewDidLayoutSubviews")
        self.makeRoundQButtons(qButtons)
        self.animateButtons(qButtons, buttonCurAngle: 2.0)

    }

    override func viewDidAppear(animated: Bool) {
     //nothing goes here
    }


}
George Asda
  • 2,029
  • 2
  • 27
  • 52

2 Answers2

4

There is no guarantee as for how many times viewDidLayoutSubviews will be called.

You can find a great discussion in this Stack Overflow post:
When is layoutSubviews called?

Pang
  • 8,605
  • 144
  • 77
  • 113
maddy
  • 3,510
  • 7
  • 38
  • 61
2

I found this article useful. A summary from what it says:

  • init does not cause layoutSubviews to be called (duh)
  • addSubview causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target view
  • setFrame intelligently calls layoutSubviews on the view having it’s frame set only if the size parameter of the frame is different
  • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and it’s superview
  • rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
  • removeFromSuperviewlayoutSubviews is called on superview only (not show in table)
g00glen00b
  • 34,293
  • 11
  • 80
  • 106
Rohan Dave
  • 253
  • 1
  • 7