How to properly add child view controller in iOS 8 with Swift
I'm going through the documentation and still seem to be of the opinion.
I have a C_SelectPhoto controller object. It has a container view. Inside the container view, I want the C_SelectPhotoControllerView child view controller to be inside it. It will just be an array of photos. However, setting the frame and adding the child view controller doesn't work. If I move the x value of the desired child view controller, there is no effect.
To understand what's going on, I was color coded everything. The container below is orange. The view the container is expecting, according to the storyboard, is yellow. The view I really want to insert is red.
Here's the storyboard:
Here is my controller code for C_SelectPhoto
class C_SelectPhoto:Controller
{
@IBOutlet weak var selectPhotoControllerView: UIView!
var _collectionViewController:C_SelectPhotoControllerView!
//TODO PERMISSION IS NEEDED BEFORE FETCHING
func initController()
{
_collectionViewController = Controller.STORYBOARD.instantiateViewControllerWithIdentifier("selectPhotoControllerView") as C_SelectPhotoControllerView
displayControllerViewController()
}
//show the photo selection
private func displayControllerViewController()
{
addChildViewController(_collectionViewController)
_collectionViewController.view.frame = CGRectMake(100, 0, 500, 500)
self.view.addSubview(_collectionViewController.view)
_collectionViewController.didMoveToParentViewController(self)
}
}
However, the result is below:
First, the yellow class shouldn't be added at all, I only need the red one (UICollectionViewController class). Second, I can tell that the red class is being added in the wrong place because its x value did not move it at all.
So my question is: How can I add the UIContainerViewController as a child to the main view controller C_SelectPhoto, but the frame of the UIContainerViewController is FIT container that I have in the main controller?
Thank!!!
NOTE. The views I'm trying to add are UICollectionViewControllers. When I add the UIViewController the cropping works very well, but as you can see when adding the UICollectionViewControllers cropping does NOT work and they are added to random offsets and do not respect my attempts to size them with frame assignments.
source to share
use the following extension to add childViewController On View
extension UIViewController {
func configureChildViewController(childController: UIViewController, onView: UIView?) {
var holderView = self.view
if let onView = onView {
holderView = onView
}
addChildViewController(childController)
holderView.addSubview(childController.view)
constrainViewEqual(holderView, view: childController.view)
childController.didMoveToParentViewController(self)
childController.willMoveToParentViewController(self)
}
func constrainViewEqual(holderView: UIView, view: UIView) {
view.translatesAutoresizingMaskIntoConstraints = false
//pin 100 points from the top of the super
let pinTop = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal,
toItem: holderView, attribute: .Top, multiplier: 1.0, constant: 0)
let pinBottom = NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal,
toItem: holderView, attribute: .Bottom, multiplier: 1.0, constant: 0)
let pinLeft = NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal,
toItem: holderView, attribute: .Left, multiplier: 1.0, constant: 0)
let pinRight = NSLayoutConstraint(item: view, attribute: .Right, relatedBy: .Equal,
toItem: holderView, attribute: .Right, multiplier: 1.0, constant: 0)
holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
}}
source to share
Updated for Swift 5+
Just one line in your view controller to add a child view controller.
Super scalable methods in the extension if you want to add it to any custom view.
public extension UIViewController {
/// Adds child view controller to the parent.
///
/// - Parameter child: Child view controller.
func add(_ child: UIViewController) {
addChild(child)
view.addSubview(child.view)
child.didMove(toParent: self)
}
/// It removes the child view controller from the parent.
func remove() {
guard parent != nil else {
return
}
willMove(toParent: nil)
removeFromParent()
view.removeFromSuperview()
}
}
How to use:
Addendum: In the view controller where you want to add a child view controller.
// let yourChildViewController = Load fro the storyboard or XIB
add(yourChildViewController)
Removal:
yourChildViewController.remove()
source to share