UIView vs Container View

So, here is the problem I'm trying to solve.

In each viewController I try to insert declarations and actual controls. I finished a couple of tutorials on raywenderlinch.com to understand how people professionally advertise in their app. They used UIViews to represent two views under the main view of the controller. Therefore, I fully understood that one subview holds ads, and the other contains the actual contents of the application. if Ad is loaded, take a screen or allow another view the entire available area.

After I returned to xcode, I began to code how I studied there. but when I dropped the UIView in the storyboard, I saw a containerView , which I think was missing when the tutorial was written.
Therefore, I am here to ask about both approaches and their pros and cons.
So basically its UIView vs containerView . How should I do, and why?
Any help would be greatly appreciated.

+8
ios xcode uiview uicontainerview iad
source share
3 answers

You use UIView when you already have a view, and you don’t need to have a dedicated view controller to create and handle interactions within it.

On the UIView help page :

The UIView object claims to be the rectangular region of its spanning superview (its parent in the hierarchy of views) and is responsible for the entire drawing in this region ...

Simplified structure: YourViewController --- (is) ---> UIView


You use UIContainerView when you need to embed another view controller in the one you already have. The built-in view controller is responsible for returning the view for the area that the UIViewContainer occupies. Therefore, your UIContainerView knows which view controller to use to render the UIView inside the area in which it occupies.

On the UIContainerView help page :

A Container View defines an area within a subgraph of a view of a view controller, which may include a child view controller.

Simplified structure: YourViewController --- (is) ---> SubViewController --- (is) ---> UIView

This SubViewController returns a view and processes its events.

+22
source share

As an alternative answer, you can also consider using instead of technical differences. For example: why use a container view?

The common use of container views is to reuse (share) the views directly in the storyboard. Previously, reusing a view required creating a separate xib file and adding that view programmatically when loading the view controller.

container view example

The above image from this is an extremely simple and straightforward guide where you will learn how to customize the container view that is shared by 2+.

A few other considerations about when to use it:

  • The navigation bar is part of the UINavigationController, which is the container view controller . So, if you want to create a custom alternative, you are probably using the container view.
  • A container can help at any time when you want to temporarily show a complex view on top of your current VC, but you cannot / do not want to represent another VC in different ways. This approach still allows you to create this temporary view in the interface builder, set automatic layout restrictions for it, etc.
  • I also found a guide explaining that there is a way to disable various kinds of containers based on the situation, allowing your VC to have a sub item that is very dynamic, but without the need to create these subkeys programmatically. The picture from this guide shows what I mean:

using several container view options

Hope this helps people who are trying to figure out when the container applies to them. If you have other examples of use, edit / add them or leave in the comments!

+3
source share

If you see in detail this kind of container of the UIView class. To get an idea of ​​why we need a containerView, you should see a part below

In most cases, the container presentation controller is similar to the content presentation controller. It manages views and content, coordinates with other objects in your application, and responds to events in the responder chain. Before designing a container controller, you should already be familiar with the design of content view controllers. Design considerations in the “Creating Custom Content Controllers” section also apply when creating containers.

for more details on the container view goto link But before you start, you must understand

and also you can check out this tutorial on how to use the container view.

That way you can use both approaches. Hope this helps you. happy coding :)

+1
source share

All Articles