Commit 531a6d39 by danieldahan

added MaterialLayout, Layout, and Align classes

parent 1af9a3a2
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
65A3F0161D07DBF30047EDD9 /* MaterialLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65A3F0151D07DBF30047EDD9 /* MaterialLayout.swift */; };
65A3F0181D07DBFD0047EDD9 /* Align.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65A3F0171D07DBFD0047EDD9 /* Align.swift */; };
961F18E81CD93E3E008927C5 /* ErrorTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961F18E71CD93E3E008927C5 /* ErrorTextField.swift */; }; 961F18E81CD93E3E008927C5 /* ErrorTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961F18E71CD93E3E008927C5 /* ErrorTextField.swift */; };
96334EF61C8B84660083986B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96334EF51C8B84660083986B /* Assets.xcassets */; }; 96334EF61C8B84660083986B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96334EF51C8B84660083986B /* Assets.xcassets */; };
963832421B88DFD80015F710 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 963832361B88DFD80015F710 /* Material.framework */; }; 963832421B88DFD80015F710 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 963832361B88DFD80015F710 /* Material.framework */; };
...@@ -195,6 +197,8 @@ ...@@ -195,6 +197,8 @@
/* End PBXContainerItemProxy section */ /* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
65A3F0151D07DBF30047EDD9 /* MaterialLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialLayout.swift; sourceTree = "<group>"; };
65A3F0171D07DBFD0047EDD9 /* Align.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Align.swift; sourceTree = "<group>"; };
961F18E71CD93E3E008927C5 /* ErrorTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorTextField.swift; sourceTree = "<group>"; }; 961F18E71CD93E3E008927C5 /* ErrorTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorTextField.swift; sourceTree = "<group>"; };
96334EF51C8B84660083986B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; }; 96334EF51C8B84660083986B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
963832361B88DFD80015F710 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Material.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 963832361B88DFD80015F710 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Material.framework; sourceTree = BUILT_PRODUCTS_DIR; };
...@@ -479,8 +483,10 @@ ...@@ -479,8 +483,10 @@
96BCB8041CB40F6C00C806FE /* Layout */ = { 96BCB8041CB40F6C00C806FE /* Layout */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
96BCB7611CB40DC500C806FE /* Grid.swift */, 65A3F0151D07DBF30047EDD9 /* MaterialLayout.swift */,
96BCB7811CB40DC500C806FE /* Layout.swift */, 96BCB7811CB40DC500C806FE /* Layout.swift */,
65A3F0171D07DBFD0047EDD9 /* Align.swift */,
96BCB7611CB40DC500C806FE /* Grid.swift */,
); );
name = Layout; name = Layout;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -919,6 +925,7 @@ ...@@ -919,6 +925,7 @@
96BCB7AA1CB40DC500C806FE /* Grid.swift in Sources */, 96BCB7AA1CB40DC500C806FE /* Grid.swift in Sources */,
96BCB7E71CB40DC500C806FE /* TextView.swift in Sources */, 96BCB7E71CB40DC500C806FE /* TextView.swift in Sources */,
96BCB7C31CB40DC500C806FE /* MaterialEdgeInset.swift in Sources */, 96BCB7C31CB40DC500C806FE /* MaterialEdgeInset.swift in Sources */,
65A3F0161D07DBF30047EDD9 /* MaterialLayout.swift in Sources */,
96BCB7CE1CB40DC500C806FE /* MaterialRadius.swift in Sources */, 96BCB7CE1CB40DC500C806FE /* MaterialRadius.swift in Sources */,
96BCB7E11CB40DC500C806FE /* BarView.swift in Sources */, 96BCB7E11CB40DC500C806FE /* BarView.swift in Sources */,
96BCB7A11CB40DC500C806FE /* BottomNavigationController.swift in Sources */, 96BCB7A11CB40DC500C806FE /* BottomNavigationController.swift in Sources */,
...@@ -934,6 +941,7 @@ ...@@ -934,6 +941,7 @@
96BCB7A61CB40DC500C806FE /* CardView.swift in Sources */, 96BCB7A61CB40DC500C806FE /* CardView.swift in Sources */,
96BCB7C91CB40DC500C806FE /* MaterialLayer.swift in Sources */, 96BCB7C91CB40DC500C806FE /* MaterialLayer.swift in Sources */,
96BCB7E21CB40DC500C806FE /* BarController.swift in Sources */, 96BCB7E21CB40DC500C806FE /* BarController.swift in Sources */,
65A3F0181D07DBFD0047EDD9 /* Align.swift in Sources */,
96BCB7DC1CB40DC500C806FE /* RaisedButton.swift in Sources */, 96BCB7DC1CB40DC500C806FE /* RaisedButton.swift in Sources */,
96BCB7DD1CB40DC500C806FE /* RobotoFont.swift in Sources */, 96BCB7DD1CB40DC500C806FE /* RobotoFont.swift in Sources */,
96BCB7D71CB40DC500C806FE /* MenuView.swift in Sources */, 96BCB7D71CB40DC500C806FE /* MenuView.swift in Sources */,
......
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Material nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
public class Align {
/// UIView context.
internal weak var context: UIView?
init(context: UIView?) {
self.context = context
}
/// Edges
public func edges(child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignToParent(v, child: child, top: top, left: left, bottom: bottom, right: right)
}
}
/// TopLeft
public func topLeft(child: UIView, top: CGFloat = 0, left: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignFromTopLeft(v, child: child, top: top, left: left)
}
}
/// TopRight
public func topRight(child: UIView, top: CGFloat = 0, right: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignFromTopRight(v, child: child, top: top, right: right)
}
}
/// BottomLeft
public func bottomLeft(child: UIView, bottom: CGFloat = 0, left: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignFromBottomLeft(v, child: child, bottom: bottom, left: left)
}
}
/// BottomRight
public func bottomRight(child: UIView, bottom: CGFloat = 0, right: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignFromBottomRight(v, child: child, bottom: bottom, right: right)
}
}
/// Top
public func top(child: UIView, top: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignFromTop(v, child: child, top: top)
}
}
/// Left
public func left(child: UIView, left: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignFromLeft(v, child: child, left: left)
}
}
/// Bottom
public func bottom(child: UIView, bottom: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignFromBottom(v, child: child, bottom: bottom)
}
}
/// Right
public func right(child: UIView, right: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.alignFromRight(v, child: child, right: right)
}
}
/// Center
public func center(child: UIView, constantX: CGFloat = 0, constantY: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.center(v, child: child, constantX: constantX, constantY: constantY)
}
}
/// CenterHorizontally
public func centerHorizontally(child: UIView, constant: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.centerHorizontally(v, child: child, constant: constant)
}
}
/// CenterVertically
public func centerVertically(child: UIView, constant: CGFloat = 0) {
if let v: UIView = context {
MaterialLayout.centerVertically(v, child: child, constant: constant)
}
}
}
/// A memory reference to the AlignKey instance for UIView extensions.
private var AlignKey: UInt8 = 0
/// MaterialLayout extension for UIView.
public extension UIView {
/// Align reference.
public private(set) var align: Align {
get {
return MaterialAssociatedObject(self, key: &AlignKey) {
return Align(context: self)
}
}
set(value) {
MaterialAssociateObject(self, key: &AlignKey, value: value)
}
}
}
...@@ -184,8 +184,8 @@ public class Grid { ...@@ -184,8 +184,8 @@ public class Grid {
self.rows = rows self.rows = rows
self.columns = columns self.columns = columns
self.spacing = spacing self.spacing = spacing
self.offset = GridOffset(grid: self) offset = GridOffset(grid: self)
self.axis = GridAxis(grid: self) axis = GridAxis(grid: self)
} }
/// Reload the button layout. /// Reload the button layout.
......
...@@ -30,24 +30,6 @@ ...@@ -30,24 +30,6 @@
import UIKit import UIKit
/// A memory reference to the Layout instance for UIView extensions.
private var LayoutKey: UInt8 = 0
/// Layout extension for UIView.
public extension UIView {
/// Grid reference.
public private(set) var layout: Layout {
get {
return MaterialAssociatedObject(self, key: &LayoutKey) {
return Layout(context: self)
}
}
set(value) {
MaterialAssociateObject(self, key: &LayoutKey, value: value)
}
}
}
public class Layout { public class Layout {
/// UIView context. /// UIView context.
internal weak var context: UIView? internal weak var context: UIView?
...@@ -55,302 +37,155 @@ public class Layout { ...@@ -55,302 +37,155 @@ public class Layout {
init(context: UIView?) { init(context: UIView?) {
self.context = context self.context = context
} }
}
public extension Layout {
/// Width /// Width
public func width(child: UIView, width: CGFloat = 0) { public func width(child: UIView, width: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.width(v, child: child, width: width) MaterialLayout.width(v, child: child, width: width)
} }
} }
/// Height /// Height
public func height(child: UIView, height: CGFloat = 0) { public func height(child: UIView, height: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.height(v, child: child, height: height) MaterialLayout.height(v, child: child, height: height)
} }
} }
/// Size /// Size
public func size(child: UIView, width: CGFloat = 0, height: CGFloat = 0) { public func size(child: UIView, width: CGFloat = 0, height: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.size(v, child: child, width: width, height: height) MaterialLayout.size(v, child: child, width: width, height: height)
} }
} }
/// Array of UIViews horizontally aligned. /// Array of UIViews horizontally aligned.
public func horizontally(children: Array<UIView>, left: CGFloat = 0, right: CGFloat = 0, spacing: CGFloat = 0) { public func horizontally(children: Array<UIView>, left: CGFloat = 0, right: CGFloat = 0, spacing: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParentHorizontally(v, children: children, left: left, right: right, spacing: spacing) MaterialLayout.alignToParentHorizontally(v, children: children, left: left, right: right, spacing: spacing)
} }
} }
/// Array of UIViews vertically aligned. /// Array of UIViews vertically aligned.
public func vertically(children: Array<UIView>, top: CGFloat = 0, bottom: CGFloat = 0, spacing: CGFloat = 0) { public func vertically(children: Array<UIView>, top: CGFloat = 0, bottom: CGFloat = 0, spacing: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParentVertically(v, children: children, top: top, bottom: bottom, spacing: spacing) MaterialLayout.alignToParentVertically(v, children: children, top: top, bottom: bottom, spacing: spacing)
} }
} }
/// Horizontally aligned. /// Horizontally aligned.
public func horizontally(child: UIView, left: CGFloat = 0, right: CGFloat = 0) { public func horizontally(child: UIView, left: CGFloat = 0, right: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParentHorizontally(v, child: child, left: left, right: right) MaterialLayout.alignToParentHorizontally(v, child: child, left: left, right: right)
} }
} }
/// Vertically aligned. /// Vertically aligned.
public func vertically(child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) { public func vertically(child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParentVertically(v, child: child, top: top, bottom: bottom) MaterialLayout.alignToParentVertically(v, child: child, top: top, bottom: bottom)
} }
} }
/// Align /// Layout
public func align(child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) { public func align(child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParent(v, child: child, top: top, left: left, bottom: bottom, right: right) MaterialLayout.alignToParent(v, child: child, top: top, left: left, bottom: bottom, right: right)
} }
} }
/// AlignFromTopLeft /// LayoutFromTopLeft
public func alignFromTopLeft(child: UIView, top: CGFloat = 0, left: CGFloat = 0) { public func alignFromTopLeft(child: UIView, top: CGFloat = 0, left: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignFromTopLeft(v, child: child, top: top, left: left) MaterialLayout.alignFromTopLeft(v, child: child, top: top, left: left)
} }
} }
/// AlignFromTopRight /// LayoutFromTopRight
public func alignFromTopRight(child: UIView, top: CGFloat = 0, right: CGFloat = 0) { public func alignFromTopRight(child: UIView, top: CGFloat = 0, right: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignFromTopRight(v, child: child, top: top, right: right) MaterialLayout.alignFromTopRight(v, child: child, top: top, right: right)
} }
} }
/// AlignFromBottomLeft /// LayoutFromBottomLeft
public func alignFromBottomLeft(child: UIView, bottom: CGFloat = 0, left: CGFloat = 0) { public func alignFromBottomLeft(child: UIView, bottom: CGFloat = 0, left: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignFromBottomLeft(v, child: child, bottom: bottom, left: left) MaterialLayout.alignFromBottomLeft(v, child: child, bottom: bottom, left: left)
} }
} }
/// AlignFromBottomRight /// LayoutFromBottomRight
public func alignFromBottomRight(child: UIView, bottom: CGFloat = 0, right: CGFloat = 0) { public func alignFromBottomRight(child: UIView, bottom: CGFloat = 0, right: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignFromBottomRight(v, child: child, bottom: bottom, right: right) MaterialLayout.alignFromBottomRight(v, child: child, bottom: bottom, right: right)
} }
} }
/// AlignFromTop /// LayoutFromTop
public func alignFromTop(child: UIView, top: CGFloat = 0) { public func alignFromTop(child: UIView, top: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignFromTop(v, child: child, top: top) MaterialLayout.alignFromTop(v, child: child, top: top)
} }
} }
/// AlignFromLeft /// LayoutFromLeft
public func alignFromLeft(child: UIView, left: CGFloat = 0) { public func alignFromLeft(child: UIView, left: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignFromLeft(v, child: child, left: left) MaterialLayout.alignFromLeft(v, child: child, left: left)
} }
} }
/// AlignFromBottom /// LayoutFromBottom
public func alignFromBottom(child: UIView, bottom: CGFloat = 0) { public func alignFromBottom(child: UIView, bottom: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignFromBottom(v, child: child, bottom: bottom) MaterialLayout.alignFromBottom(v, child: child, bottom: bottom)
} }
} }
/// AlignFromRight /// LayoutFromRight
public func alignFromRight(child: UIView, right: CGFloat = 0) { public func alignFromRight(child: UIView, right: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.alignFromRight(v, child: child, right: right) MaterialLayout.alignFromRight(v, child: child, right: right)
} }
} }
/// Center /// Center
public func center(child: UIView, constantX: CGFloat = 0, constantY: CGFloat = 0) { public func center(child: UIView, constantX: CGFloat = 0, constantY: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.center(v, child: child, constantX: constantX, constantY: constantY) MaterialLayout.center(v, child: child, constantX: constantX, constantY: constantY)
} }
} }
/// CenterHorizontally /// CenterHorizontally
public func centerHorizontally(child: UIView, constant: CGFloat = 0) { public func centerHorizontally(child: UIView, constant: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.centerHorizontally(v, child: child, constant: constant) MaterialLayout.centerHorizontally(v, child: child, constant: constant)
} }
} }
/// CenterVertically /// CenterVertically
public func centerVertically(child: UIView, constant: CGFloat = 0) { public func centerVertically(child: UIView, constant: CGFloat = 0) {
if let v: UIView = context { if let v: UIView = context {
Layout.centerVertically(v, child: child, constant: constant) MaterialLayout.centerVertically(v, child: child, constant: constant)
} }
} }
} }
/// Layout instance extension. /// A memory reference to the LayoutKey instance for UIView extensions.
public extension Layout { private var LayoutKey: UInt8 = 0
/// Width
public class func width(parent: UIView, child: UIView, width: CGFloat = 0) { /// MaterialLayout extension for UIView.
prepareForConstraint(parent, child: child) public extension UIView {
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: width)) /// Layout reference.
} public private(set) var layout: Layout {
get {
/// Height return MaterialAssociatedObject(self, key: &LayoutKey) {
public class func height(parent: UIView, child: UIView, height: CGFloat = 0) { return Layout(context: self)
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: height))
}
/// Size
public class func size(parent: UIView, child: UIView, width: CGFloat = 0, height: CGFloat = 0) {
Layout.width(parent, child: child, width: width)
Layout.height(parent, child: child, height: height)
}
/// AlignToParentHorizontally
public class func alignToParentHorizontally(parent: UIView, children: Array<UIView>, left: CGFloat = 0, right: CGFloat = 0, spacing: CGFloat = 0) {
prepareForConstraint(parent, children: children)
if 0 < children.count {
parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1, constant: left))
for i in 1..<children.count {
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Left, relatedBy: .Equal, toItem: children[i - 1], attribute: .Right, multiplier: 1, constant: spacing))
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Width, relatedBy: .Equal, toItem: children[0], attribute: .Width, multiplier: 1, constant: 0))
}
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1, constant: -right))
}
}
/// AlignToParentVertically
public class func alignToParentVertically(parent: UIView, children: Array<UIView>, top: CGFloat = 0, bottom: CGFloat = 0, spacing: CGFloat = 0) {
prepareForConstraint(parent, children: children)
if 0 < children.count {
parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1, constant: top))
for i in 1..<children.count {
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Top, relatedBy: .Equal, toItem: children[i - 1], attribute: .Bottom, multiplier: 1, constant: spacing))
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Height, relatedBy: .Equal, toItem: children[0], attribute: .Height, multiplier: 1, constant: 0))
}
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1, constant: -bottom))
}
}
/// AlignToParentHorizontally
public class func alignToParentHorizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1, constant: left))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1, constant: -right))
}
/// AlignToParentVertically
public class func alignToParentVertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1, constant: top))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1, constant: -bottom))
}
/// AlignToParent
public class 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)
alignToParentVertically(parent, child: child, top: top, bottom: bottom)
}
/// AlignFromTopLeft
public class func alignFromTopLeft(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0) {
alignFromTop(parent, child: child, top: top)
alignFromLeft(parent, child: child, left: left)
}
/// AlignFromTopRight
public class func alignFromTopRight(parent: UIView, child: UIView, top: CGFloat = 0, right: CGFloat = 0) {
alignFromTop(parent, child: child, top: top)
alignFromRight(parent, child: child, right: right)
}
/// AlignFromBottomLeft
public class func alignFromBottomLeft(parent: UIView, child: UIView, bottom: CGFloat = 0, left: CGFloat = 0) {
alignFromBottom(parent, child: child, bottom: bottom)
alignFromLeft(parent, child: child, left: left)
}
/// AlignFromBottomRight
public class func alignFromBottomRight(parent: UIView, child: UIView, bottom: CGFloat = 0, right: CGFloat = 0) {
alignFromBottom(parent, child: child, bottom: bottom)
alignFromRight(parent, child: child, right: right)
}
/// AlignFromTop
public class func alignFromTop(parent: UIView, child: UIView, top: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1, constant: top))
}
/// AlignFromLeft
public class func alignFromLeft(parent: UIView, child: UIView, left: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1, constant: left))
}
/// AlignFromBottom
public class func alignFromBottom(parent: UIView, child: UIView, bottom: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1, constant: -bottom))
}
/// AlignFromRight
public class func alignFromRight(parent: UIView, child: UIView, right: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1, constant: -right))
}
/// Center
public class func center(parent: UIView, child: UIView, constantX: CGFloat = 0, constantY: CGFloat = 0) {
centerHorizontally(parent, child: child, constant: constantX)
centerVertically(parent, child: child, constant: constantY)
}
/// CenterHorizontally
public class func centerHorizontally(parent: UIView, child: UIView, constant: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .CenterX, relatedBy: .Equal, toItem: parent, attribute: .CenterX, multiplier: 1, constant: constant))
}
/// CenterVertically
public class func centerVertically(parent: UIView, child: UIView, constant: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .CenterY, relatedBy: .Equal, toItem: parent, attribute: .CenterY, multiplier: 1, constant: constant))
}
/// Constraint
public class func constraint(format: String, options: NSLayoutFormatOptions, metrics: Dictionary<String, AnyObject>?, views: Dictionary<String, AnyObject>) -> Array<NSLayoutConstraint> {
for (_, a) in views {
if let v: UIView = a as? UIView {
v.translatesAutoresizingMaskIntoConstraints = false
} }
} }
return NSLayoutConstraint.constraintsWithVisualFormat( set(value) {
format, MaterialAssociateObject(self, key: &LayoutKey, value: value)
options: options,
metrics: metrics,
views: views
)
}
/// prepareForConstraint
private class func prepareForConstraint(parent: UIView, child: UIView) {
if parent != child.superview {
parent.addSubview(child)
}
child.translatesAutoresizingMaskIntoConstraints = false
}
/// prepareForConstraint
private class func prepareForConstraint(parent: UIView, children: [UIView]) {
for v in children {
prepareForConstraint(parent, child: v)
} }
} }
} }
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Material nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
/// MaterialLayout
public struct MaterialLayout {
/// Width
public static func width(parent: UIView, child: UIView, width: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: width))
}
/// Height
public static func height(parent: UIView, child: UIView, height: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: height))
}
/// Size
public static func size(parent: UIView, child: UIView, width: CGFloat = 0, height: CGFloat = 0) {
MaterialLayout.width(parent, child: child, width: width)
MaterialLayout.height(parent, child: child, height: height)
}
/// AlignToParentHorizontally
public static func alignToParentHorizontally(parent: UIView, children: Array<UIView>, left: CGFloat = 0, right: CGFloat = 0, spacing: CGFloat = 0) {
prepareForConstraint(parent, children: children)
if 0 < children.count {
parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1, constant: left))
for i in 1..<children.count {
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Left, relatedBy: .Equal, toItem: children[i - 1], attribute: .Right, multiplier: 1, constant: spacing))
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Width, relatedBy: .Equal, toItem: children[0], attribute: .Width, multiplier: 1, constant: 0))
}
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1, constant: -right))
}
}
/// AlignToParentVertically
public static func alignToParentVertically(parent: UIView, children: Array<UIView>, top: CGFloat = 0, bottom: CGFloat = 0, spacing: CGFloat = 0) {
prepareForConstraint(parent, children: children)
if 0 < children.count {
parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1, constant: top))
for i in 1..<children.count {
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Top, relatedBy: .Equal, toItem: children[i - 1], attribute: .Bottom, multiplier: 1, constant: spacing))
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .Height, relatedBy: .Equal, toItem: children[0], attribute: .Height, multiplier: 1, constant: 0))
}
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1, constant: -bottom))
}
}
/// AlignToParentHorizontally
public static func alignToParentHorizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1, constant: left))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1, constant: -right))
}
/// AlignToParentVertically
public static func alignToParentVertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1, constant: top))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1, constant: -bottom))
}
/// AlignToParent
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)
alignToParentVertically(parent, child: child, top: top, bottom: bottom)
}
/// AlignFromTopLeft
public static func alignFromTopLeft(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0) {
alignFromTop(parent, child: child, top: top)
alignFromLeft(parent, child: child, left: left)
}
/// AlignFromTopRight
public static func alignFromTopRight(parent: UIView, child: UIView, top: CGFloat = 0, right: CGFloat = 0) {
alignFromTop(parent, child: child, top: top)
alignFromRight(parent, child: child, right: right)
}
/// AlignFromBottomLeft
public static func alignFromBottomLeft(parent: UIView, child: UIView, bottom: CGFloat = 0, left: CGFloat = 0) {
alignFromBottom(parent, child: child, bottom: bottom)
alignFromLeft(parent, child: child, left: left)
}
/// AlignFromBottomRight
public static func alignFromBottomRight(parent: UIView, child: UIView, bottom: CGFloat = 0, right: CGFloat = 0) {
alignFromBottom(parent, child: child, bottom: bottom)
alignFromRight(parent, child: child, right: right)
}
/// AlignFromTop
public static func alignFromTop(parent: UIView, child: UIView, top: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Top, relatedBy: .Equal, toItem: parent, attribute: .Top, multiplier: 1, constant: top))
}
/// AlignFromLeft
public static func alignFromLeft(parent: UIView, child: UIView, left: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Left, relatedBy: .Equal, toItem: parent, attribute: .Left, multiplier: 1, constant: left))
}
/// AlignFromBottom
public static func alignFromBottom(parent: UIView, child: UIView, bottom: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Bottom, relatedBy: .Equal, toItem: parent, attribute: .Bottom, multiplier: 1, constant: -bottom))
}
/// AlignFromRight
public static func alignFromRight(parent: UIView, child: UIView, right: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1, constant: -right))
}
/// 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)
}
/// CenterHorizontally
public static func centerHorizontally(parent: UIView, child: UIView, constant: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .CenterX, relatedBy: .Equal, toItem: parent, attribute: .CenterX, multiplier: 1, constant: constant))
}
/// CenterVertically
public static func centerVertically(parent: UIView, child: UIView, constant: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .CenterY, relatedBy: .Equal, toItem: parent, attribute: .CenterY, multiplier: 1, constant: constant))
}
/// Constraint
public static func constraint(format: String, options: NSLayoutFormatOptions, metrics: Dictionary<String, AnyObject>?, views: Dictionary<String, AnyObject>) -> Array<NSLayoutConstraint> {
for (_, a) in views {
if let v: UIView = a as? UIView {
v.translatesAutoresizingMaskIntoConstraints = false
}
}
return NSLayoutConstraint.constraintsWithVisualFormat(
format,
options: options,
metrics: metrics,
views: views
)
}
/// prepareForConstraint
private static func prepareForConstraint(parent: UIView, child: UIView) {
if parent != child.superview {
parent.addSubview(child)
}
child.translatesAutoresizingMaskIntoConstraints = false
}
/// prepareForConstraint
private static func prepareForConstraint(parent: UIView, children: [UIView]) {
for v in children {
prepareForConstraint(parent, child: v)
}
}
}
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