Commit e9be01ee by danieldahan

updated Layout API

parent 531a6d39
......@@ -97,9 +97,10 @@ class AppLeftViewController: UIViewController {
nameLabel.font = RobotoFont.mediumWithSize(18)
view.layout.size(profileView, width: 72, height: 72)
view.layout.alignFromTopLeft(profileView, top: 30, left: (view.bounds.width - 72) / 2)
view.layout.align.topLeft(profileView, top: 30, left: (view.bounds.width - 72) / 2)
view.addSubview(nameLabel)
view.layout.align
Layout.alignFromTop(view, child: nameLabel, top: 130)
Layout.alignToParentHorizontally(view, child: nameLabel, left: 20, right: 20)
// view.layout.alignFromTop(nameLabel, top: 130)
......@@ -115,7 +116,7 @@ class AppLeftViewController: UIViewController {
tableView.separatorStyle = .None
// Use Layout to easily align the tableView.
view.layout.align(tableView, top: 170)
view.layout.align.edges(tableView, top: 170)
}
}
......
......@@ -140,7 +140,7 @@ class AppMenuController: MenuController {
menuView.delegate = self
view.layout.size(menuView, width: baseSize.width, height: baseSize.height)
view.layout.alignFromBottomRight(menuView, bottom: 65, right: 16)
view.layout.align.bottomRight(menuView, bottom: 65, right: 16)
}
}
......
......@@ -241,7 +241,7 @@ class RecipesViewController: UIViewController {
tableView.delegate = self
// Use Layout to easily align the tableView.
view.layout.align(tableView)
view.layout.align.edges(tableView)
}
}
......@@ -286,7 +286,7 @@ extension RecipesViewController: UITableViewDataSource {
label.textColor = MaterialColor.grey.darken1
label.text = "Favorites"
header.layout.align(label, left: 24)
header.layout.align.edges(label, left: 24)
return header
}
......
......@@ -70,7 +70,7 @@ class RecommendationViewController: UIViewController {
tableView.delegate = self
// Use Layout to easily align the tableView.
view.layout.align(tableView)
view.layout.align.edges(tableView)
}
}
......@@ -115,7 +115,7 @@ extension RecommendationViewController: UITableViewDataSource {
label.textColor = MaterialColor.grey.darken1
label.text = "Recommendations"
header.layout.align(label, left: 24)
header.layout.align.edges(label, left: 24)
return header
}
......
......@@ -7,8 +7,6 @@
objects = {
/* 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 */; };
96334EF61C8B84660083986B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96334EF51C8B84660083986B /* Assets.xcassets */; };
963832421B88DFD80015F710 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 963832361B88DFD80015F710 /* Material.framework */; };
......@@ -197,8 +195,6 @@
/* End PBXContainerItemProxy 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>"; };
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; };
......@@ -483,9 +479,7 @@
96BCB8041CB40F6C00C806FE /* Layout */ = {
isa = PBXGroup;
children = (
65A3F0151D07DBF30047EDD9 /* MaterialLayout.swift */,
96BCB7811CB40DC500C806FE /* Layout.swift */,
65A3F0171D07DBFD0047EDD9 /* Align.swift */,
96BCB7611CB40DC500C806FE /* Grid.swift */,
);
name = Layout;
......@@ -925,7 +919,6 @@
96BCB7AA1CB40DC500C806FE /* Grid.swift in Sources */,
96BCB7E71CB40DC500C806FE /* TextView.swift in Sources */,
96BCB7C31CB40DC500C806FE /* MaterialEdgeInset.swift in Sources */,
65A3F0161D07DBF30047EDD9 /* MaterialLayout.swift in Sources */,
96BCB7CE1CB40DC500C806FE /* MaterialRadius.swift in Sources */,
96BCB7E11CB40DC500C806FE /* BarView.swift in Sources */,
96BCB7A11CB40DC500C806FE /* BottomNavigationController.swift in Sources */,
......@@ -941,7 +934,6 @@
96BCB7A61CB40DC500C806FE /* CardView.swift in Sources */,
96BCB7C91CB40DC500C806FE /* MaterialLayer.swift in Sources */,
96BCB7E21CB40DC500C806FE /* BarController.swift in Sources */,
65A3F0181D07DBFD0047EDD9 /* Align.swift in Sources */,
96BCB7DC1CB40DC500C806FE /* RaisedButton.swift in Sources */,
96BCB7DD1CB40DC500C806FE /* RobotoFont.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)
}
}
}
......@@ -233,7 +233,7 @@ public class BottomTabBar : UITabBar {
super.didMoveToSuperview()
if autoLayoutToSuperview {
if let v: UIView = superview {
v.layout.alignFromBottom(self)
v.layout.align.bottom(self)
v.layout.horizontally(self)
}
}
......
......@@ -314,7 +314,7 @@ public class CardView : MaterialPulseView {
h += "[\(k)]"
layout.alignFromBottom(b, bottom: contentInset.bottom + leftButtonsInset.bottom)
layout.align.bottom(b, bottom: contentInset.bottom + leftButtonsInset.bottom)
i += 1
}
......@@ -343,7 +343,7 @@ public class CardView : MaterialPulseView {
h += "-(right_left)-"
}
layout.alignFromBottom(b, bottom: contentInset.bottom + rightButtonsInset.bottom)
layout.align.bottom(b, bottom: contentInset.bottom + rightButtonsInset.bottom)
i -= 1
}
......
......@@ -378,7 +378,7 @@ public class ImageCardView : MaterialPulseView {
verticalFormat += "-[titleLabel]"
views["titleLabel"] = v
} else {
layout.alignFromTop(v, top: contentInset.top + titleLabelInset.top)
layout.align.top(v, top: contentInset.top + titleLabelInset.top)
}
layout.horizontally(v, left: contentInset.left + titleLabelInset.left, right: contentInset.right + titleLabelInset.right)
}
......@@ -417,7 +417,7 @@ public class ImageCardView : MaterialPulseView {
h += "[\(k)]"
layout.alignFromBottom(b, bottom: contentInset.bottom + leftButtonsInset.bottom)
layout.align.bottom(b, bottom: contentInset.bottom + leftButtonsInset.bottom)
i += 1
}
......@@ -446,7 +446,7 @@ public class ImageCardView : MaterialPulseView {
h += "-(right_left)-"
}
layout.alignFromBottom(b, bottom: contentInset.bottom + rightButtonsInset.bottom)
layout.align.bottom(b, bottom: contentInset.bottom + rightButtonsInset.bottom)
i -= 1
}
......
/*
* 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)
}
}
}
......@@ -401,7 +401,7 @@ public class TextView: UITextView {
internal func reloadView() {
if let p = placeholderLabel {
removeConstraints(constraints)
layout.align(p,
layout.align.edges(p,
top: textContainerInset.top,
left: textContainerInset.left + textContainer.lineFragmentPadding,
bottom: textContainerInset.bottom,
......
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