--The container's job is to position the root view of each child view controller within the bounds of its own view.
”视图控制器容器“ 的工作就是把“子视图控制器”的根视图 放在容器“自身视图”的范围之内。
--Always use the UIKit container view controllers to implement the common navigation models:
UIKit提供了下面这几种 视图控制器容器 给你使用,一般来讲够用了。
UINavigationController(link)manages a stack of child view controllers in a navigation interface. Pushing a new view controller onto the stack replaces the previous view controller. Popping a view controller reveals the view controller underneath.
UISplitViewController(link)manages two child view controllers side-by-side (when space is available) in a split-view interface. (When space is constrained, the system displays those view controllers in a navigation interface.) This type of interface is also known as a master-detail interface.
UITabBarController(link)displays a row of buttons along the bottom of a tab-bar interface. Selecting a button displays the child view controller associated with that button.
UIPageViewControllermanages an ordered sequence of child view controllers, presenting only one or two at a time, in a paged interface. The user navigates between those view controllers by swiping or tapping.
--Most custom view controllers you create are content view controllers—that is, the view controller owns all of its views and manages interactions with those views.
--Use content view controllers to present your app’s custom content onscreen, and use your view controller object to manage the transfer of data to and from your custom views.
--Constraints are rules that specify how to size and position each view relative to its parent or sibling view, and they ensure that your views automatically adapt to different environments and devices. For more information, see View Layout(link).
--When you instantiate a view controller, UIKit reconnects any outlets that you configured in your storyboard. UIKit reestablishes these connections before calling your view controller’s viewDidLoad() method, so you can access the objects in those properties from that method.
--Controls use the target-action design pattern to report user interactions, and some views post notifications or call delegate methods in response to changes.
--A view controller needs to know about many of these interactions so it can update its views, and you have several ways to make that happen:
一个视图控制器可以通过下面的几种方式来获知用户的多种交互行为,从而对视图进行更改:
Implement delegate and action methods in your view controller. This option is simple and easy to implement, but it offers less flexiblity and makes it harder to test and validate your code.
Implement delegate and action methods in a class extension on your view controller. This option separates your event-handling code from the rest of your view controller, making it easier to test and validate that code.
Implement delegate and action methods in dedicated objects that then forward relevant information to your view controller. This option offers the most flexibility and reusability. The separation of responsibilities also makes it easier to write unit tests.
--If your view controller requires custom initialization beyond what a coder object can provide, you can instantiate it programmatically using the instantiateInitialViewController(creator:) method of UIStoryboard.
--When you present a view controller onscreen, UIKit must first load and configure the corresponding views, which it does using the following sequence of steps:
--UIKit notifies the owning view controller when its views are about to appear onscreen, and updates the layout of those views to fit the current environment, by calling the following methods in order:
--When making changes, you may use the view controller's traitCollection property to access information about the current environment, such as the display scale or the vertical and horizontal size classes.
--In iOS 12 and earlier, UIKit only provides a partial set of traits at load time, and doesn't provide a complete set of traits until the viewWillAppear(_:) method. For more information about the available traits, see UITraitCollection(link).
--Every window has a root view controller, which provides the initial content for your window. Presenting a new view controller changes that content by installing a new set of views in the window.
--UIKit solves this problem with the show(_:sender:) and showDetailViewController(_:sender:) methods of UIViewController, which present the view controller in the most appropriate way for the current context.
--When you call the show(_:sender:) or showDetailViewController(_:sender:) method, UIKit determines the most appropriate context for the presentation.Specifically, it calls the targetViewController(forAction:sender:) method to search for a parent view controller that implements the corresponding show method.
Embed a View Controller Inside a Container View Controller
--Embedding a child view controller presents it using a container-specific approach. For example, a navigation controller initially positions the child view controller offscreen and then animates it into position onscreen.
--The standard UIKit container view controllers work with segues and the show(_:sender:) and showDetailViewController(_:sender:) methods to embed view controllers as children.
--Use the methods in the following table to perform one-time configuration of your view controller, for example when restoring your app's UI to a previous state.
--Use modal presentations to create temporary interruptions in your app's workflow, such as prompting the user for important information.
模态是指 打断当前的app工作流 来显式视图,例如提示客户输入重要信息时。
--A modal presentation covers the current view controller wholly or partially, depending on the presentation style you use.
模态的显示 是覆盖 整个屏幕还是部分屏幕 取决于当前环境的显示 的风格。确切来讲就是trait environment。To configure a --modal presentation, create the new view controller and call the present(_:animated:completion:) method.
你可以通过调用 VC 的present方法来进行模态显示。
--To change the styles, modify the modalPresentationStyle and modalTransitionStyle properties of the view controller you present.
--Container view controllers promote better encapsulation by separating out your content from how you display that content onscreen.
container 将“你的视图内容”与你展示内容的“方式”分离,从而提供了更好的封装性。
--Unlike a content view controller that displays your app’s data, a container view controller displays other view controllers, arranging them onscreen and handling navigation between them.
// Create a child view controller and add it to the current view controller.
let storyboard = UIStoryboard(name : "Main", bundle : .main) ;
if let viewController = storyboard.instantiateViewController(identifier : “imageViewController") as? ImageViewController {
// Add the view controller to the container.
addChild(viewController)
view.addSubview(viewController.view)
// Create and activate the constraints for the child’s view.
onscreenConstraints = configureConstraintsForContainedView(containedView : viewController.view, stage : .onscreen)
NSLayoutConstraint.activate(onscreenConstraints)
// Notify the child view controller that the move is complete.
viewController.didMove(toParent: self)
}
★-- UIKit normally routes information to each of your app’s view controllers independently.When a container-child relationship exists, UIKit routes many requests through the container view controller first, giving it a chance to alter the behavior for any child view controllers.
4、Call the child’s removeFromParent() method to finalize the end of the container-child relationship.
第四步、调用子VC的removeFromParent方法来完成容纳关系的终极,没有第五步了。
★--Breaking a container-child relationship tells UIKit that your container view controller is no longer displaying the child’s content. You can still maintain other references to the child view controller. For example, UINavigationController manages a stack of child view controllers, but it maintains a container-child relationship with only one or two of those children at any given time.
Embed a Child View Controller in Your Storyboard UI
在storyboard中嵌入子VC
额,直接看图吧:
--A container view is a proxy view that stands in for the content of a child view controller. When you add one to your interface, it looks like a normal view, but it has an attached view controller.
--After creating the new view controllers, UIKit adds them as children of the original view controller you requested. You don’t need to call addChild(_:) yourself.
CallsetOverrideTraitCollection(_:forChild:)to change the traits of your child view controllers. For example, you might designate a child view controller as always horizontally or vertically compact.
OverridechildForScreenEdgesDeferringSystemGestures or childForHomeIndicatorAutoHiddento let a child view controller determine the behavior for system gestures.
修改VC的childForScreenEdgesDeferringSystemGestures or childForHomeIndicatorAutoHidden计算属性,这样可以让子VC来决定响应系统手势的行为。
OverrideallowedChildrenForUnwinding(from:)to limit the set of child view controllers that are targets of an unwind segue action.