UIViewController相关

2017-09-13  本文已影响6人  楼上那只猫

Managing View Layout

During the layout process, UIKit notifies you at several points so that you can perform additional layout-related tasks. Use these notifications to modify your layout constraints or to make final tweaks to the layout after the layout constraints have been applied. During the layout process, UIKit does the following for each affected view controller:

  1. Updates the trait collections of the view controller and its views, as needed
  2. Calls the view controller’s viewWillLayoutSubviews
    method
  3. Calls the containerViewWillLayoutSubviews
    method of the current UIPresentationController
    object.
  4. Calls the layoutSubviews
    method of view controller’s root view.
    The default implementation of this method computes the new layout information using the available constraints. The method then traverses the view hierarchy and calls layoutSubviews
    for each subview.
  5. Applies the computed layout information to the views.
  6. Calls the view controller’s viewDidLayoutSubviews
    method
  7. Calls the containerViewDidLayoutSubviews
    method of the current UIPresentationController
    object.

通常会重写下面的两个方法来做额外的布局操作

viewWillLayoutSubviews
viewDidLayoutSubviews

View controllers can use the viewWillLayoutSubviews and viewDidLayoutSubviews methods to perform additional updates that might impact the layout process. Before layout, you might add or remove views, update the size or position of views, update constraints, or update other view-related properties. After layout, you might reload table data, update the content of other views, or make final adjustments to the size and position of views.

Container View Controller

In almost every way, a container view controller is like any other content view controller in that it manages a root view and some content. The difference is that a container view controller gets part of its content from other view controllers. The content it gets is limited to the other view controllers’ views, which it embeds inside its own view hierarchy. The container view controller sets the size and position of any embedded views, but the original view controllers still manage the content inside those views

简单来说,一个容器类的controller,它的content是来自其他controller,这些controller由它来管理,同时,这些controller的view还由他们自身去管理。容器类的controller只简单设置这些view的size和position

向一个 controller 中添加子 controller

    ChildViewController * cv = [[ChildViewController alloc] init];
    [self addChildViewController:cv];
    cv.view.frame = CGRectMake(20, 20, 200, 300);
    cv.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:cv.view];
    [cv didMoveToParentViewController:self];

In the preceding example, notice that you call only the didMoveToParentViewController: method of the child. That is because the addChildViewController: method calls the child’s willMoveToParentViewController: method for you. The reason that you must call the didMoveToParentViewController: method yourself is that the method cannot be called until after you embed the child’s view into your container’s view hierarchy.

移除子 controller

  1. Call the child’s willMoveToParentViewController: method with the value nil
  2. Remove any constraints that you configured with the child’s root view.
  3. Remove the child’s root view from your container’s view hierarchy.
  4. Call the child’s removeFromParentViewController
    method to finalize the end of the parent-child relationship
[cv willMoveToParentViewController:nil];
[cv.view removeFromSuperview];
[cv removeFromParentViewController];

Suggestions for Building a Container View Controller

Access only the root view of a child view controller. A container should access only the root view of each child—that is, the view returned by the child’s view
property. It should never access any of the child’s other views.
Child view controllers should have minimal knowledge of their container. A child view controller should focus on its own content. If the container allows its behavior to be influenced by a child, it should use the delegationdesign pattern to manage those interactions.
Design your container using regular views first.

Delegating Control to a Child View Controller

应该是让子 controller 的 statusbar 决定父 controller 的 bar 样式
Let a child view controller determine the status bar style. To delegate the status bar appearance to a child, override one or both of the childViewControllerForStatusBarStyle
and childViewControllerForStatusBarHidden
methods in your container view controller.
Let the child specify its own preferred size. A container with a flexible layout can use the child’s own preferredContentSize
property to help determine the size of the child

上一篇下一篇

猜你喜欢

热点阅读