Skip to content

Instantly share code, notes, and snippets.

@surakamy
Last active March 13, 2017 19:33
Show Gist options
  • Save surakamy/89988a6d1540cbcd823cab025e346e3d to your computer and use it in GitHub Desktop.
Save surakamy/89988a6d1540cbcd823cab025e346e3d to your computer and use it in GitHub Desktop.
Extension of UIViewController with helpers methods to simplify embedding child view controllers. <Swift 3.0.2>
/*
MIT License
Copyright © 2017 Dmytro Kholodov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import UIKit
/// Predefined Layouts for an embedded controller
enum BindLayoutOptions {
/// Hidden. View of embedded controller is invisible. It can propagate
/// other view into the partent controller.
case zero
/// Fill size of the parent controller.
case fill
/// Custom callback. Custom layout and custom setup.
typealias Callback = (_ childView: UIView) -> Void
case custom(Callback)
}
///
/// UIViewController helpers to add and remove child view controllers.
///
extension UIViewController {
/// Add child view controller with specified layout presets.
/// - Parameters:
/// - child: controller to be added as a child controller
/// - layout: predefined layout
func addController(embedded: UIViewController, layout: BindLayoutOptions = .fill) {
addChildViewController(embedded)
embedded.willMove(toParentViewController: self)
setupView(child: embedded.view, layout: layout)
embedded.didMove(toParentViewController: self)
}
/// Remove child view controller from the parent controller.
/// - Parameters:
/// - child: a controller to be added as a child controller
func removeController(embedded: UIViewController) {
embedded.willMove(toParentViewController: nil)
embedded.view.removeFromSuperview()
embedded.removeFromParentViewController()
}
/// Layout the child view in the superview
/// - Parameters:
/// - child: view to be added as a child controller
/// - layout: predefined layout
private func setupView(child: UIView, layout: BindLayoutOptions) {
view.addSubview(child)
switch layout {
case .zero:
child.frame = CGRect.zero
break
case .fill:
break
case .custom(let callback):
callback(child)
break
}
}
}
@surakamy
Copy link
Author

surakamy commented Mar 13, 2017

Examples

`

    // Fill parent view
    addController(embedded: UIViewController())
    
    // Hidden view of child controller
    addController(embedded: UIViewController(), layout: .zero)
    
    
    // Custom layout
    addController(embedded: UIViewController(), layout:  .custom { $0.frame = CGRect(x: 0, y: 0, width: 100, height: 100) } )

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment