When the autolayout constraints set frames during view controller life cycle? When the autolayout constraints set frames during view controller life cycle? xcode xcode

When the autolayout constraints set frames during view controller life cycle?


The viewDidAppear method is called at the end of the view life cycle. So if you change the constraints here, it will always be visible.

If the change you want to do is a one time event, then you may do so in the method viewWillAppear. But this won't always help because the view drawing may not always be finished by this time. So a better option is to place the code in viewWillLayoutSubviews or viewDidLayoutSubviews.

NOTE: viewWillLayoutSubviews or viewDidLayoutSubviews will be called multiple times in a view's life cycle, such as orientation change or moving in and out of another view or any frame change for that matter.
If the code change you want to put here is a one time event, then please make sure to use flags.

eg:-

- (void)viewWillLayoutSubviews {        if(firstTime) {        // put your constraint change code here.    }}

Hope this helps! :)


As name suggests, viewDidLayoutSubviews is called when the view of your viewController has just finished its laying out and you can assume that at that dynamic height your views are in their correct places/frames according to your autolayout constraints.

Swift :    override func viewDidLayoutSubviews() {       // Set your constraint here    }Objective C :   -(void)viewDidLayoutSubviews {   // Set your constraint here   }


I am not sure what you are actually trying to do in viewDidLayoutSubviews(). I always use to customise view by modifying layout constraint values overriding this method.

    // Called to notify the view controller that its view has just laid out its subviews    override func viewDidLayoutSubviews() {        //Write your code here, any constraint modification etc.       super.viewDidLayoutSubviews()    }