Skip to content

Instantly share code, notes, and snippets.

@lostincode
Last active August 29, 2015 14:15
Show Gist options
  • Save lostincode/657596ff88a0f8f0ba17 to your computer and use it in GitHub Desktop.
Save lostincode/657596ff88a0f8f0ba17 to your computer and use it in GitHub Desktop.
Child View Controller
//
// BaseViewController.swift
// Child Container ViewController Methods
//
// Created by Bill Richards on 2/11/15.
// Copyright (c) 2015 Bill Richards. All rights reserved.
//
import UIKit
extension UIViewController {
func displayContentViewControllerInView(viewController: UIViewController!, view: UIView!) {
self.addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.view.setTranslatesAutoresizingMaskIntoConstraints(false)
let superview = view
superview.addConstraints([
NSLayoutConstraint(
item: viewController.view,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: superview,
attribute: NSLayoutAttribute.Top,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: viewController.view,
attribute: NSLayoutAttribute.Bottom,
relatedBy: NSLayoutRelation.Equal,
toItem: superview,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: viewController.view,
attribute: NSLayoutAttribute.Left,
relatedBy: NSLayoutRelation.Equal,
toItem: superview,
attribute: NSLayoutAttribute.Left,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: viewController.view,
attribute: NSLayoutAttribute.Right,
relatedBy: NSLayoutRelation.Equal,
toItem: superview,
attribute: NSLayoutAttribute.Right,
multiplier: 1.0,
constant: 0
)
])
viewController.didMoveToParentViewController(self)
}
func hideContentViewController(viewController: UIViewController?) {
if let vc = viewController as UIViewController! {
vc.willMoveToParentViewController(nil)
vc.view.removeFromSuperview()
vc.removeFromParentViewController()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment