iOS View Controller Programming Guide---Implementing a container View Controller

Source: Internet
Author: User
Tags compact uikit

A container view controller is a way to combine the contents of multiple view controllers into a single user interface. The container view controller is often used to make navigation easier, creating a new user interface type based on what already exists. For example, the container view controller in Uikit includes Uinavigationcontroller,uitabbarcontroller and Uisplitviewcontroller, They all make it easier to switch and navigate the user interface between different view sections.


Design a custom container view Controller

In almost all respects, a container view controller, like any other content view controller, manages a root view and some content. The difference between the two is that the container view controller obtains some of the content from other view controllers, and the resulting content is limited to views of other view controllers embedded in the container view controller's own view hierarchy. The container view controller sets the size and position of any inline views, but the original view controller still manages the contents of those views.


When designing your own container view controller, you should always understand the relationship between the container and contained container view controllers. The relationships between view controllers can help inform how their content should be displayed on the screen, and how your container should manage them internally. In the design process, you should first ask yourself the following questions:

-What role does the children of your container and container play?

-How many children can be displayed at the same time?

-What is the relationship between the sibling view controllers?

-How do I add or remove a child view controller in a container?

Can the size and position of the-children be changed? What are the conditions under which such a change will occur?

-Does the container itself provide decorative or navigation-related views?

-What kind of communication should be provided between the container and its children? Does the container need to specify an event to its children report in addition to the standard events defined in class Uiviewcontroller?

-Can I configure the display of the container in different ways? If so, how should it be configured?


After you have defined the rules between various different objects, implementing a container view controller is fairly straightforward. The only requirement for UIKit is that you need to establish a formal parent-child relationship between the container view controller and any of its child view controllers. The parent-child relationship ensures that the children receives relevant information from the system. In addition, most of the actual work takes place during the layout and management of the views it contains, and each container view controller is different at this point. You can place the view anywhere in the container View Controller content area, or you can set the view to any size you want within that area. You can also add custom views to the view hierarchy in the navigation to provide decorations or help.


Example:navigation Controller

A Uinavigationcontroller object supports navigation through a hierarchical set of data. A navigation interface renders one child view controller at a time. The navigation bar at the top of the interface shows the current position in the data hierarchy, as well as a back button to return to the previous layer. Navigating down to the data layer is determined by the child view controller and may involve the use of tables and buttons.


The navigation between the view controllers is managed by the navigation controller and its children. When the user interacts with a child view Controller's button or table row, the child View controller requires the navigation controller to push a new view controller to the view, the Child view Controller is responsible for the configuration of the content of the new view controller, and the Navigation view Controller is responsible for managing the animations transition. The navigation controller is also responsible for managing the navigation bar, which shows a back button that is used to dismiss the topmost view controller.


Figure 5-1 shows the structure between a navigation controller and its view. The vast majority of content areas are populated by the topmost child view controller, which occupies only a small portion of the navigation bar.

Figure 5-1 Structure of the navigation interface


In the compact and regular environments, the Navigation view controller displays only one child view controller at a time. The Navigation view controller is responsible for resizing its child view to fit the effective screen space.


Example:split View Controller

The Uisplitviewcontroller object presents the contents of the two view controllers in a master-slave arrangement, in which a view controller (master) determines the details of the other view controllers displayed to the interface. The visibility of the two view controllers can be configured, but they are all dictated by the current environment. In a horizontal regular environment, the split view controller can display two view controllers at the side of the edge, or it can hide the master and display it when needed. In a compact environment, the Split view controller displays only one view controller at a time.


Figure 5-2 shows the structure between a split view controller and its view view in a horizontal regular environment. The split view controller itself has its own container view by default. In this example, two views are displayed with side-by-side display, the size of the child view is configurable, and the view is visible in the diagram.

Figure 5-2 a split view interface



Configuring a view in Interface Builder

To create a parent-child container relationship at design time, add a container view object to your storyboard screen, 5-3. A container View object is a placeholder object that represents the contents of a sub-view controller. Use this view in a container to set the size and position of the root view and other related views.

Figure 5-3 Adding a container view in Interface Builder


When you load a view controller for one or more container views, Interface Builder also loads the child view controller associated with this view. The child view and the parent view must be initialized at the same time, so that a suitable parent-child relationship can be built up.

If you do not apply interface Builder to build your parent-child container relationship, you must create this relationship in a coded manner by adding each child to the container view controller, in detail, in Adding a child View Controller to Your Content.


Implement a custom Container view Controller

In order to implement a container view controller, you must establish a relationship between the view Controller and its child view controllers. It is necessary to establish this parent-child relationship before you try to manage the view and any of its child view controllers. By doing so, you can let uikit know that your view controller is managing the size and location of its child views. You can create this relationship by interface Builder or by encoding. When you create this relationship in a coded way, as part of building the view controller, you should display the Add and remove child view controllers.


Add a child view controller to your content

To encode a child view controller into your content, create a parent-child relationship between the associated view controllers by following these steps:

1. Call the Addchildviewcontroller: method of the container view controller. This method tells Uikit that your container view controller is managing the associated View's child view controller.

2. Add the child's root view to the container's view hierarchy. In this process, you should always remember to set the size and position of the child's frame.

3. Add several constraints to manage the size and location of the child root view.

4. Call the Didmovetoparentviewcontroller: method of the child view controller.


Listing5-1 shows how a container is embedded in a sub-view controller into its container. After establishing the parent-child relationship, the container sets its child's frame, adding the child's view to its view hierarchy. It is important to set the child's view frame size to ensure that the view is displayed correctly in your container. After adding the view, the container calls the child's Didmovetoparentviewcontroller: method to give the sub-view controller an opportunity to respond to the view ownership change. (Note that the child here refers to the sub-view controller in the container View Controller)

Listing5-1 Adding a child view controller to a container

-(void) Displaycontentcontroller: (uiviewcontroller*) content {   [self addchildviewcontroller:content];   Content.view.frame = [self frameforcontentcontroller];   [Self.view AddSubview:self.currentClientView];   [Content didmovetoparentviewcontroller:self];}
In this example above, notice that you just called the child's Didmovetoparentviewcontroller: method. That's because Addchildviewcontroller: The method calls the child's Willmovetoparentviewcontroller: Method for you. You must call the Didmovetoparentviewcontroller yourself: This method cannot be called until you embed the child's view into the container's view hierarchy.


When using automatic layout, when you add a child view to the view hierarchy of your container, the constraint is established between the container and its children. Your constraint should only affect the size and position of the child root view, not the root view in the child view hierarchy or any other view.


Remove a child view Controller

To remove a child view controller from your content, use these steps to remove the parent-child relationship between the view controllers:

1. Call the Willmovetoparentviewcontroller: method with a child value of nil.

2. Remove any constraints that you have set on the child's root view.

3. Remove the child's root view from the view level of your container.

4. At the end of the parent-child relationship, call the child's Removefromparentviewcontroller method to end.

Removing a sub-view controller permanently cuts the connection between child and parent. Remove it only if you don't need to refer to a child view controller. For example, when a new view controller is push to the navigation stack, the Navigation view controller does not remove its current child view controller. Remove the child view controller only when it is popped out of the navigation stack.


Listing 5-2 shows how to move a child view controller that removes it from the container. Call the Willmovetoparentviewcontroller: method with the parameter nil to give the child view controller a chance to prepare for the change. The Removefromparentviewcontroller method also invokes the Didmovetoparentviewcontroller: method of the Child View controller, which is passed to the method with a value of nil for the parameter. Finally, set the parent view controller to point to nil to remove the view of the child view controller from your container.


Listing5-2 Removing a child view controller from the container

-(void) Hidecontentcontroller: (uiviewcontroller*) content {   [content Willmovetoparentviewcontroller:nil];   [Content.view Removefromsuperview];   [Content Removefromparentviewcontroller];}



Switch between child view controllers

When you want animate a child view controller to replace another, include a newly added view controller to the transition animation process while removing the other child view controller. Before animations, make sure that two child view controllers are part of the container content and that the current child view Controller knows that it is about to leave. During animations, move the view of the new Child view controller to the appropriate location and remove the previous view of the child view controller. After the animation is complete, the child view controller is removed.


Listing5-3 shows how to swap two sub-view controllers using the transition animation. In this example, the new view controller is animate to a rectangular area occupied by the currently existing child view controller, which is removed from the screen. After the animations is complete, the completion block removes the child view controller from the container. In this example, the TransitionFromViewController:toViewController:duration:options:animations:completion: method automatically updates the container's view hierarchy, So you don't need to manually add and remove these views.

Listing5-3 switching between two child view controllers


Managing appearance updates for children

After adding a child view controller to the container, the container automatically sends appearance-related information to the child view controller. This is the normal behavior you want, because it ensures that all events are sent normally. However, sometimes the default behavior may send these events in an unreasonable order to the container. For example, if multiple children change their view state at the same time, you may want to merge these changes so that appearance callbacks occur simultaneously in a more reasonable order.

In order to take over the responsibility of appearance callbacks, overload shouldAutomaticallyForwardAppearanceMethods The method in your container view controller and return no, as in listing5-4. Return no and let Uikit know that your container view controller uses its own appearance to notify it of children changes.

Listing5-4 Disabling automatic appearance forwarding

-(BOOL) shouldautomaticallyforwardappearancemethods {    return NO;}

When a appearance switch occurs, the child view controller is called as appropriate  beginAppearanceTransition:animated:Method or Endappearancetransition method. For example, if you have a single view controller reference in your container, pass a child property. Your container should send the information in the following listing5-5 to the child view controller.

Listing 5-5 Forwarding appearance messages when the container appears or disappears

-(void) Viewwillappear: (BOOL) animated {    [Self.child beginappearancetransition:yes animated:animated];}-(void) Viewdidappear: (bool) animated {    [Self.child endappearancetransition];}-(void) Viewwilldisappear: (BOOL) animated {    [self.child beginappearancetransition:no animated:animated];} -(void) Viewdiddisappear: (BOOL) animated {    [Self.child endappearancetransition];}


recommendations for building a container view controller

Take some time to design, develop and test a new container view controller. Although the individual behavior of the container is straightforward, the container as a whole is quite complex. Consider the following recommendations when you implement your own container class:

-Simply access the root view of a child view controller. The container should only access the root view of each child view controller, that is, the view that the Quilt View controller's view properties return. It is easy to never access other views of the child view controller.

-Child view controllers should have minimal knowledge of their containers. The child view Controller should focus on its own content, and if the container allows its behavior to be affected by another child view controller, then it should use the delegation design pattern to manage these interactions.
-First use the regular view to design your container. using the regular view (instead of the view from the Child View Controller) gives you an opportunity to test layout constraints and animated transitions in a simple environment. When the regular view is working as expected, look for a suitable view in your child view controller to exchange it.


Delegate control for Child view controllers

A container view controller can delegate some aspects of its own appearance to one or more of its child view controllers. You can use the following methods to delegate control:

-let a Child view controller determine the style of the status bar. In order to delegate the status bar appearance to a child view controller, you can overload one or two childviewcontrollerforstatusbarstyle  AND  childviewcontrollerforstatusbarhidden   method.

-The Komi View controller specifies its own preferred size. A container with flexible layouts can use the Child View controller's own Preferredcontentsize property to help determine the size of the child view.





iOS View Controller Programming Guide---Implementing a container View Controller

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.