Reputation: 3132
what is the difference between a subview and a container view. I have a piece of code that is successfully working by adding subview programmatically. But I want to be able to lay out the subview in the editor not in code. The only thing I could find was containerview. What is the difference and can they be used interchangeably.
Thanks.
Upvotes: 1
Views: 711
Reputation: 2908
You use UIView when you already have a view and you do not need to have a dedicated view controller to build and handle interactions within it.
From the UIView help page:
UIView object claims a rectangular region of its enclosing superview (its parent in the view hierarchy) and is responsible for all drawing in that region ...
Simplified structure: YourViewController ---(has)---> UIView
You use UIContainerView when you need to embed another view controller in the one that you already have. The embedded view controller is in charge of returning a view for the region that the UIViewContainer occupies. Therefore, your UIContainerView knows which view controller to use to render UIView inside the region it occupies.
From the UIContainerView help page:
Container View defines a region within a view controller's view subgraph that can include a child view controller.
Simplified structure: YourViewController ---(has)---> SubViewContoller ---(has)---> UIView
That SubViewController returns a view and handles its events.
Last if you want to learn how to layout subviews, i cannot explain that over here, so you might need to go through one of the tutorials. https://www.raywenderlich.com/113388/storyboards-tutorial-in-ios-9-part-1
Upvotes: 2