3

I would like to update constraitn constants and multiplier and I would like to update these constraints in right view controller life cycle event

  • loadView

  • viewDidLoad

  • viewWillAppear

  • viewWillLayoutSubviews

  • viewDidLayoutSubviews

  • viewDidAppear

There are some posts saying its better to update constraints at UpdateViewConstraints method of viewcontroller

There is so much confusion related surrounding this.

Somebody please explain

Shankar Naik
  • 401
  • 5
  • 16
  • 1
    You haven't provided enough context for your question. Under what circumstances do you need to update your costraints? When something changes? Always, as part of setting up your view controller? – Duncan C Oct 05 '17 at 20:23
  • When the view controller is loaded I want to set up the constraints for the views in viewcontroller – Shankar Naik Oct 05 '17 at 20:26
  • sometime after the button click I want to change some view's constraints – Shankar Naik Oct 05 '17 at 20:27
  • Then it sounds like you should update the constraints in the handler for the button tap event. – Paulw11 Oct 05 '17 at 20:37
  • @Paulw11 - how about first one "when the view controller is loaded I want to set up the constraints for the views in viewcontroller "? – Shankar Naik Oct 05 '17 at 21:12
  • If you are using a storyboard, set them in the storyboard. If you are adding the views programmatically, add the constraints when you add the views – Paulw11 Oct 05 '17 at 21:30
  • Can you edit your question to clarify what you are asking about? – Duncan C Oct 06 '17 at 17:23

1 Answers1

1

Create your constraints in your storyboard. Control-drag from your constraints into your source file to create outlets to those constraints.

Then, in your button IBAction method, update the constant values to the constraints and call layoutIfNeeded().

As Paul said in his comment, if you're creating your views in code, also create your constraints in code and save them to properties in your view controller, then use those properties in your button IBAction like described above.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks I would like to know in where can I update constraints in view controller life cycle method ? basically constrains that is to do with viewcontroller's initial view appearance like height,width etc.. I would like to update it in the right place so that when view controller is loaded the view appearance doesn't causes any shake in the UI – Shankar Naik Oct 10 '17 at 18:22
  • 2
    If you need to adjust constraints before your view displays, put the code in viewWillAppear(), and call layoutIfNeeded() once you've adjusted your constraints. – Duncan C Oct 10 '17 at 19:31
  • But the view's movement appears and it looks awkward. for exaple by defatult its height could be 100 px but in viewwillappear it may set it to 200px and this visible to naked eyes for example 100 px it appears then to the eye it appears to become 200 px and it makes me think may be in a different place I have to do – Shankar Naik Oct 19 '17 at 17:42