Skip to content

Instantly share code, notes, and snippets.

@doozMen
Created June 10, 2015 10:52
Show Gist options
  • Save doozMen/15981fef2e025ad63608 to your computer and use it in GitHub Desktop.
Save doozMen/15981fef2e025ad63608 to your computer and use it in GitHub Desktop.
A swift vertical layout alternative
//
// Layout.swift
//
// Created by Stijn Willems on 10/06/15.
// Copyright (c) 2015 dooZ. All rights reserved.
//
import Foundation
typealias LeftRigth = (left:UIView, right: UIView?, aspectRatioToViewController: CGFloat?)
class Layout {
typealias Container = (container:UIView, left:UIView, right: UIView?, aspectRatioToViewController: CGFloat?)
//MARK: - Vertical layout
class vertical {
class func fitInViewController(viewController: UIViewController, views: [LeftRigth]) {
var viewsWithContainer = [Container]()
for view in views {
let container = UIView.newAutoLayoutView()
viewsWithContainer.append((container:container, left: view.left, right: view.right, aspectRatioToViewController: view.aspectRatioToViewController))
}
addViewAndPinBottomAndTopToViewController(viewController, views:viewsWithContainer)
addViewsAndPinBottomAndTopToView(viewsWithContainer)
pinLeftRightAndDimensionToContainer(viewsWithContainer)
}
private class func pinLeftRightAndDimensionToContainer(views:[Container]){
for view in views {
view.left.autoPinEdge(.Trailing, toEdge: .Leading, ofView: view.right)
view.left.autoPinEdge(.Leading, toEdge: .Leading, ofView: view.container)
view.right?.autoPinEdge(.Trailing, toEdge: .Trailing, ofView: view.container)
view.left.autoMatchDimension(.Width, toDimension: .Width, ofView: view.container, withMultiplier: 0.2)
}
}
private class func addViewsAndPinBottomAndTopToView(views: [Container]){
for view in views {
view.container.addSubview(view.left)
if let right = view.right {
view.container.addSubview(right)
}
view.left.autoPinEdge(.Top, toEdge: .Top, ofView: view.container)
view.left.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: view.container)
view.right?.autoPinEdge(.Top, toEdge: .Top, ofView: view.container)
view.right?.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: view.container)
}
}
private class func addViewAndPinBottomAndTopToViewController(viewController: UIViewController, views: [Container]){
for view in views {
view.container.setTranslatesAutoresizingMaskIntoConstraints(false)
viewController.view.addSubview(view.container)
view.container.autoPinEdge(.Leading, toEdge: .Leading, ofView: viewController.view)
view.container.autoPinEdge(.Trailing, toEdge: .Trailing, ofView: viewController.view)
}
let firstContainer = views.first!.container
let lastContainer = views.last!.container
var middleViews = views.filter{$0.container != firstContainer}
firstContainer.autoPinToTopLayoutGuideOfViewController(viewController, withInset: 0)
if let multiplier = views.first?.aspectRatioToViewController {
firstContainer.autoMatchDimension(.Height, toDimension: .Height, ofView: viewController.view, withMultiplier: multiplier)
}
lastContainer.autoPinToBottomLayoutGuideOfViewController(viewController, withInset: 0)
var previousView = firstContainer
for view in middleViews {
view.container.autoPinEdge(.Top, toEdge: .Bottom, ofView: previousView)
if let multiplier = view.aspectRatioToViewController {
view.container.autoMatchDimension(.Height, toDimension: .Height, ofView: viewController.view, withMultiplier: multiplier)
}
previousView = view.container
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment