iOS Developer Library — Pre-Release

Developer

UIKit Framework Reference UIContentContainer Protocol Reference

Options
Deployment Target:

On This Page
Language:

UIContentContainer

The methods of the UIContentContainer protocol help you adapt the contents of your view controllers to size and trait changes. All UIViewController and UIPresentationController objects provide default implementations for the methods of this protocol. When creating your own custom view controller or presentation controller, you can override the default implementations to make adjustments to your content. For example, you might use these methods to adjust the size or position of any child view controllers.

When overriding the methods of this protocol, you should generally call super to give UIKit a chance to perform any default behaviors. View controllers and presentation controllers perform their own adjustments when these methods are called. Calling super ensures that UIKit is able to continue making the changes it needs to adapt your interface to changes.

Inheritance


Not Applicable

Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 8.0 and later.
  • Notifies the container that the size of its view is about to change. (required)

    Declaration

    Swift

    func viewWillTransitionToSize(_ size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)

    Objective-C

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

    Parameters

    size

    The new size for the container’s view.

    coordinator

    The transition coordinator object managing the size change. You can use this object to animate your changes or get information about the transition that is in progress.

    Discussion

    UIKit calls this method before changing the size of a presented view controller’s view. You can override this method in your own objects and use it to perform additional tasks related to the size change. For example, a container view controller might use this method to override the traits of its embedded child view controllers. Use the provided coordinator object to animate any changes you make.

    If you override this method in your custom view controllers, always call super at some point in your implementation so that UIKit can forward the size change message appropriately. View controllers forward the size change message to their views and child view controllers. Presentation controllers forward the size change to their presented view controller.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • Notifies the container that its trait collection changed. (required)

    Declaration

    Swift

    func willTransitionToTraitCollection(_ newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)

    Objective-C

    - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

    Parameters

    newCollection

    The traits to be applied to the container.

    coordinator

    The transition coordinator object managing the trait change. You can use this object to animate any changes or to get information about the transition that is in progress.

    Discussion

    UIKit calls this method before changing the current object’s traits and before calling the traitCollectionDidChange: method of any affected views and view controllers. Implementors of this method can use it to adapt the interface based on the values in the newCollection parameter. A common use of this method is to make changes to the high-level presentation style when the current size class changes. For example, a container view controller that manages multiple child view controllers might change the number of child view controllers it displays onscreen when the size class changes. A standard view controller might use this method to change the constraints on the views it manages. Use the provided coordinator object to animate any changes you make.

    If you override this method in your own objects, always call super at some point in your implementation so that UIKit can forward the trait changes to the associated presentation controller and to any child view controllers. View controllers forward the trait change message to their child view controllers. Presentation controllers forward the trait change to their presented view controller.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • Returns the size of the specified child view controller’s content. (required)

    Declaration

    Swift

    func sizeForChildContentContainer(_ container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize

    Objective-C

    - (CGSize)sizeForChildContentContainer:(id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize

    Parameters

    container

    The child view controller.

    parentSize

    The size of the parent view controller.

    Return Value

    The size to apply to the child view controller.

    Discussion

    Container view controllers use this method to return the sizes for their child view controllers. UIKit calls the method as part of the default implementation of the viewWillTransitionToSize:withTransitionCoordinator: method for view controllers. It calls the method once for each child view controller embedded in the view controller. If you are implementing a custom container view controller, you should override this method and use it to return the sizes of the contained children.

    View controllers and presentation controllers return the value in parentSize by default.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • Notifies an interested controller that the preferred content size of one of its children changed.

    Declaration

    Swift

    func preferredContentSizeDidChangeForChildContentContainer(_ container: UIContentContainer)

    Objective-C

    - (void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container

    Parameters

    container

    The child view controller who’s preferred content size has changed.

    Discussion

    UIKit calls this method on a container view controller when the preferredContentSize property of one of its child view controllers changes. Similarly, if the view controller is managed by a presentation controller, UIKit calls this method on the presentation controller to let it know of the change. The parent view controller or presentation controller can use this method to initiate layout adjustments based on the new size information.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • Notifies the container that a child view controller was resized using auto layout.

    Declaration

    Swift

    func systemLayoutFittingSizeDidChangeForChildContentContainer(_ container: UIContentContainer)

    Objective-C

    - (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container

    Parameters

    container

    The child view controller that received the resizing message.

    Discussion

    This method is called when a view controller that does not use auto layout has a child view controller that uses auto layout and the child view controller is resized. When the child view controller responds to the systemLayoutSizeFittingSize: method, the systemLayoutFittingSizeDidChangeForChildContentContainer: method is sent to the parent view controller.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • The preferred size for the container’s content. (required) (read-only)

    Declaration

    Swift

    var preferredContentSize: CGSize { get }

    Objective-C

    @property(nonatomic, readonly) CGSize preferredContentSize

    Discussion

    The UIViewController class implements a writable version of this property.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.