Search code examples
iosuiviewuiviewcontrollersubviews

IOS where to remove subviews programmatically in ViewController


I'm quite new to IOS so I'm sorry if my question is obvious. I have set a ViewController's view in storyboard which contains other subviews.

In viewWillAppear I update these subviews depending on the object I passed to this ViewController. This object can have nil attributes and in this case I want to remove these subviews.

What is the right place to remove these subviews and is there a difference in terms of efficiency?

  • viewDidLoad
  • viewWillAppear
  • or viewWillLayoutSubviews ?

and will the constraints set to these removed objects also be removed?

Thx


Solution

  • The constraints will definitely be removed. However, it is possible to save the constraints in an array and add them back again in the future.

    I would suggest making changes to the views( orientation, visibility, geometry ) in the viewWillLayoutSubviews method. You wouldn't want to do anything expensive in the ViewWillAppear method, because at that point the view is ready to be displayed to the user and it could impact how quickly the view appears to load for the user.