Commit 003323d6 by Daniel Dahan

Merge pull request #420 from mohpor/development

Updates MaterialLayout with new syntax and centering additions
parents d2d0214f 956a928a
...@@ -43,6 +43,7 @@ class ViewController: UIViewController { ...@@ -43,6 +43,7 @@ class ViewController: UIViewController {
prepareView() prepareView()
prepareAlignToParentHorizontallyExample() prepareAlignToParentHorizontallyExample()
prepareAlignToParentVerticallyExample() prepareAlignToParentVerticallyExample()
prepareCenterExample()
} }
/// General preparation statements. /// General preparation statements.
...@@ -141,5 +142,49 @@ class ViewController: UIViewController { ...@@ -141,5 +142,49 @@ class ViewController: UIViewController {
print(v.frame) print(v.frame)
} }
} }
// Lays out test views to the center of different axes
private func prepareCenterExample() {
let length: CGFloat = 100
let labelCX = UILabel()
labelCX.backgroundColor = MaterialColor.grey.base
labelCX.text = "centerX"
labelCX.textAlignment = .Center
labelCX.layer.cornerRadius = length / 2.0
labelCX.clipsToBounds = true
view.addSubview(labelCX)
MaterialLayout.width(view, child: labelCX, width: length)
MaterialLayout.height(view, child: labelCX, height: length)
MaterialLayout.centerHorizontally(view, child: labelCX)
let labelCY = UILabel()
labelCY.backgroundColor = MaterialColor.grey.base
labelCY.text = "centerY"
labelCY.textAlignment = .Center
labelCY.layer.cornerRadius = length / 2.0
labelCY.clipsToBounds = true
view.addSubview(labelCY)
MaterialLayout.width(view, child: labelCY, width: length)
MaterialLayout.height(view, child: labelCY, height: length)
MaterialLayout.centerVertically(view, child: labelCY)
let labelCXY = UILabel()
labelCXY.backgroundColor = MaterialColor.grey.base
labelCXY.text = "centerXY"
labelCXY.textAlignment = .Center
labelCXY.layer.cornerRadius = length / 2.0
labelCXY.clipsToBounds = true
view.addSubview(labelCXY)
MaterialLayout.width(view, child: labelCXY, width: length)
MaterialLayout.height(view, child: labelCXY, height: length)
MaterialLayout.center(view, child: labelCXY)
}
} }
...@@ -32,115 +32,144 @@ import UIKit ...@@ -32,115 +32,144 @@ import UIKit
public struct MaterialLayout { public struct MaterialLayout {
/// Width /// Width
public static func width(parent: UIView, child: UIView, width: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func width(parent: UIView, child: UIView, width: CGFloat = 0) {
let metrics: Dictionary<String, AnyObject> = ["width" : width] prepareForConstraint([child])
let views: Dictionary<String, AnyObject> = ["child" : child] parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1.0, constant: width))
parent.addConstraints(constraint("H:[child(width)]", options: options, metrics: metrics, views: views))
} }
/// Height /// Height
public static func height(parent: UIView, child: UIView, height: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func height(parent: UIView, child: UIView, height: CGFloat = 0) {
let metrics: Dictionary<String, AnyObject> = ["height" : height] prepareForConstraint([child])
let views: Dictionary<String, AnyObject> = ["child" : child] parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: height))
parent.addConstraints(constraint("V:[child(height)]", options: options, metrics: metrics, views: views))
} }
/// Size /// Size
public static func size(parent: UIView, child: UIView, width: CGFloat = 0, height: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func size(parent: UIView, child: UIView, width: CGFloat = 0, height: CGFloat = 0) {
MaterialLayout.width(parent, child: child, width: width) MaterialLayout.width(parent, child: child, width: width)
MaterialLayout.height(parent, child: child, height: height) MaterialLayout.height(parent, child: child, height: height)
} }
/// AlignToParentHorizontally /// AlignToParentHorizontally
public static func alignToParentHorizontally(parent: UIView, children: Array<UIView>, left: CGFloat = 0, right: CGFloat = 0, spacing: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignToParentHorizontally(parent: UIView, children: Array<UIView>, left: CGFloat = 0, right: CGFloat = 0, spacing: CGFloat = 0) {
prepareForConstraint(children)
if 0 < children.count { if 0 < children.count {
var format: String = "H:|-(left)-" parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1.0, constant: left))
var i: Int = 1 if 1 < children.count {
var views: Dictionary<String, UIView> = Dictionary<String, UIView>() for i in 1...(children.count - 1) {
for v in children { parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Left, relatedBy: .Equal, toItem: children[i-1], attribute: .Right, multiplier: 1.0, constant: spacing))
let k: String = "view\(i)" parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Width, relatedBy: .Equal, toItem: children[0], attribute: .Width, multiplier: 1.0, constant: 0.0))
i += 1
views[k] = v
format += i > children.count ? "[\(k)(==view1)]-(right)-|" : "[\(k)(==view1)]-(spacing)-"
} }
parent.addConstraints(constraint(format, options: options, metrics: ["left" : left, "right": right, "spacing": spacing], views: views)) }
parent.addConstraint(NSLayoutConstraint(item: children[children.count-1], attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1.0, constant: -right))
} }
} }
/// AlignToParentVertically /// AlignToParentVertically
public static func alignToParentVertically(parent: UIView, children: Array<UIView>, top: CGFloat = 0, bottom: CGFloat = 0, spacing: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignToParentVertically(parent: UIView, children: Array<UIView>, top: CGFloat = 0, bottom: CGFloat = 0, spacing: CGFloat = 0) {
prepareForConstraint(children)
if 0 < children.count { if 0 < children.count {
var format: String = "V:|-(top)-" parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1.0, constant: top))
var i: Int = 1 if 1 < children.count {
var views: Dictionary<String, UIView> = Dictionary<String, UIView>() for i in 1...(children.count - 1) {
for v in children { parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Top, relatedBy: .Equal, toItem: children[i-1], attribute: .Bottom, multiplier: 1.0, constant: spacing))
let k: String = "view\(i)" parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Height, relatedBy: .Equal, toItem: children[0], attribute: .Height, multiplier: 1.0, constant: 0.0))
i += 1 }
views[k] = v
format += i > children.count ? "[\(k)(==view1)]-(bottom)-|" : "[\(k)(==view1)]-(spacing)-"
} }
parent.addConstraints(constraint(format, options: options, metrics: ["top" : top, "bottom": bottom, "spacing": spacing], views: views)) parent.addConstraint(NSLayoutConstraint(item: children[children.count-1], attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1.0, constant: -bottom))
} }
} }
/// AlignToParentHorizontally /// AlignToParentHorizontally
public static func alignToParentHorizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignToParentHorizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0) {
parent.addConstraints(constraint("H:|-(left)-[child]-(right)-|", options: options, metrics: ["left": left, "right": right], views: ["child" : child])) prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1.0, constant: left))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1.0, constant: -right))
} }
/// AlignToParentVertically /// AlignToParentVertically
public static func alignToParentVertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignToParentVertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) {
parent.addConstraints(constraint("V:|-(top)-[child]-(bottom)-|", options: options, metrics: ["bottom": bottom, "top": top], views: ["child" : child])) prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1.0, constant: top))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1.0, constant: -bottom))
} }
/// AlignToParent /// AlignToParent
public static func alignToParent(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignToParent(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) {
alignToParentHorizontally(parent, child: child, left: left, right: right) alignToParentHorizontally(parent, child: child, left: left, right: right)
alignToParentVertically(parent, child: child, top: top, bottom: bottom) alignToParentVertically(parent, child: child, top: top, bottom: bottom)
} }
/// AlignFromTopLeft /// AlignFromTopLeft
public static func alignFromTopLeft(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignFromTopLeft(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0) {
alignFromTop(parent, child: child, top: top) alignFromTop(parent, child: child, top: top)
alignFromLeft(parent, child: child, left: left) alignFromLeft(parent, child: child, left: left)
} }
/// AlignFromTopRight /// AlignFromTopRight
public static func alignFromTopRight(parent: UIView, child: UIView, top: CGFloat = 0, right: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignFromTopRight(parent: UIView, child: UIView, top: CGFloat = 0, right: CGFloat = 0) {
alignFromTop(parent, child: child, top: top) alignFromTop(parent, child: child, top: top)
alignFromRight(parent, child: child, right: right) alignFromRight(parent, child: child, right: right)
} }
/// AlignFromBottomLeft /// AlignFromBottomLeft
public static func alignFromBottomLeft(parent: UIView, child: UIView, bottom: CGFloat = 0, left: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignFromBottomLeft(parent: UIView, child: UIView, bottom: CGFloat = 0, left: CGFloat = 0) {
alignFromBottom(parent, child: child, bottom: bottom) alignFromBottom(parent, child: child, bottom: bottom)
alignFromLeft(parent, child: child, left: left) alignFromLeft(parent, child: child, left: left)
} }
/// AlignFromBottomRight /// AlignFromBottomRight
public static func alignFromBottomRight(parent: UIView, child: UIView, bottom: CGFloat = 0, right: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignFromBottomRight(parent: UIView, child: UIView, bottom: CGFloat = 0, right: CGFloat = 0) {
alignFromBottom(parent, child: child, bottom: bottom) alignFromBottom(parent, child: child, bottom: bottom)
alignFromRight(parent, child: child, right: right) alignFromRight(parent, child: child, right: right)
} }
/// AlignFromTop /// AlignFromTop
public static func alignFromTop(parent: UIView, child: UIView, top: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignFromTop(parent: UIView, child: UIView, top: CGFloat = 0) {
parent.addConstraints(constraint("V:|-(top)-[child]", options: options, metrics: ["top" : top], views: ["child" : child])) prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1.0, constant: top))
} }
/// AlignFromLeft /// AlignFromLeft
public static func alignFromLeft(parent: UIView, child: UIView, left: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignFromLeft(parent: UIView, child: UIView, left: CGFloat = 0) {
parent.addConstraints(constraint("H:|-(left)-[child]", options: options, metrics: ["left" : left], views: ["child" : child])) prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1.0, constant: left))
} }
/// AlignFromBottom /// AlignFromBottom
public static func alignFromBottom(parent: UIView, child: UIView, bottom: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignFromBottom(parent: UIView, child: UIView, bottom: CGFloat = 0) {
parent.addConstraints(constraint("V:[child]-(bottom)-|", options: options, metrics: ["bottom" : bottom], views: ["child" : child])) prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1.0, constant: -bottom))
} }
/// AlignFromRight /// AlignFromRight
public static func alignFromRight(parent: UIView, child: UIView, right: CGFloat = 0, options: NSLayoutFormatOptions = []) { public static func alignFromRight(parent: UIView, child: UIView, right: CGFloat = 0) {
parent.addConstraints(constraint("H:[child]-(right)-|", options: options, metrics: ["right" : right], views: ["child" : child])) prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1.0, constant: -right))
}
/// CenterHorizontally
public static func centerHorizontally(parent: UIView, child: UIView, constant: CGFloat = 0) {
prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .CenterX, relatedBy: .Equal, toItem: parent, attribute: .CenterX, multiplier: 1.0, constant: constant))
}
/// CenterVertically
public static func centerVertically(parent: UIView, child: UIView, constant: CGFloat = 0) {
prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .CenterY, relatedBy: .Equal, toItem: parent, attribute: .CenterY, multiplier: 1.0, constant: constant))
}
/// Center
public static func center(parent: UIView, child: UIView, constantX: CGFloat = 0, constantY: CGFloat = 0) {
centerHorizontally(parent, child: child, constant: constantX)
centerVertically(parent, child: child, constant: constantY)
}
/// prepareForConstraint
private static func prepareForConstraint(views: [UIView]) {
for v in views {
v.translatesAutoresizingMaskIntoConstraints = false
}
} }
/// Constraint /// Constraint
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment