简体   繁体   中英

Why viewDidLayoutSubviews is called twice only on first run?

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
    }


}

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM