Commit cbae939a by M. Porooshani

Reworked the Layout class for chainables and did some cleanup

parent 2b3fa494
...@@ -97,10 +97,12 @@ class AppLeftViewController: UIViewController { ...@@ -97,10 +97,12 @@ class AppLeftViewController: UIViewController {
nameLabel.font = RobotoFont.mediumWithSize(18) nameLabel.font = RobotoFont.mediumWithSize(18)
view.layout.size(profileView, width: 72, height: 72) view.layout.size(profileView, width: 72, height: 72)
view.layout.align.topLeft(profileView, top: 30, left: (view.bounds.width - 72) / 2) // view.layout.align.topLeft(profileView, top: 30, left: (view.bounds.width - 72) / 2)
view.layout(profileView).top(30).left((view.bounds.width - 72) / 2)
view.addSubview(nameLabel) view.addSubview(nameLabel)
view.layout.align
Layout.alignFromTop(view, child: nameLabel, top: 130) Layout.alignFromTop(view, child: nameLabel, top: 130)
Layout.alignToParentHorizontally(view, child: nameLabel, left: 20, right: 20) Layout.alignToParentHorizontally(view, child: nameLabel, left: 20, right: 20)
// view.layout.alignFromTop(nameLabel, top: 130) // view.layout.alignFromTop(nameLabel, top: 130)
...@@ -116,7 +118,7 @@ class AppLeftViewController: UIViewController { ...@@ -116,7 +118,7 @@ class AppLeftViewController: UIViewController {
tableView.separatorStyle = .None tableView.separatorStyle = .None
// Use Layout to easily align the tableView. // Use Layout to easily align the tableView.
view.layout.align.edges(tableView, top: 170) view.layout.edges(tableView, top: 170)
} }
} }
......
...@@ -157,7 +157,7 @@ class AppMenuController: MenuController { ...@@ -157,7 +157,7 @@ class AppMenuController: MenuController {
menuView.delegate = self menuView.delegate = self
view.layout.size(menuView, width: baseSize.width, height: baseSize.height) view.layout.size(menuView, width: baseSize.width, height: baseSize.height)
view.layout.align.bottomRight(menuView, bottom: menuViewInset, right: menuViewInset) view.layout.bottomRight(menuView, bottom: menuViewInset, right: menuViewInset)
} }
/// Prepare tabBarItem. /// Prepare tabBarItem.
......
...@@ -169,7 +169,7 @@ class RecipesViewController: UIViewController { ...@@ -169,7 +169,7 @@ class RecipesViewController: UIViewController {
tableView.delegate = self tableView.delegate = self
// Use Layout to easily align the tableView. // Use Layout to easily align the tableView.
view.layout.align.edges(tableView) view.layout.edges(tableView)
} }
} }
...@@ -214,7 +214,7 @@ extension RecipesViewController: UITableViewDataSource { ...@@ -214,7 +214,7 @@ extension RecipesViewController: UITableViewDataSource {
label.textColor = MaterialColor.grey.darken1 label.textColor = MaterialColor.grey.darken1
label.text = "Favorites" label.text = "Favorites"
header.layout.align.edges(label, left: 24) header.layout.edges(label, left: 24)
return header return header
} }
......
...@@ -70,7 +70,7 @@ class RecommendationViewController: UIViewController { ...@@ -70,7 +70,7 @@ class RecommendationViewController: UIViewController {
tableView.delegate = self tableView.delegate = self
// Use Layout to easily align the tableView. // Use Layout to easily align the tableView.
view.layout.align.edges(tableView) view.layout.edges(tableView)
} }
} }
...@@ -115,7 +115,7 @@ extension RecommendationViewController: UITableViewDataSource { ...@@ -115,7 +115,7 @@ extension RecommendationViewController: UITableViewDataSource {
label.textColor = MaterialColor.grey.darken1 label.textColor = MaterialColor.grey.darken1
label.text = "Recommendations" label.text = "Recommendations"
header.layout.align.edges(label, left: 24) header.layout.edges(label, left: 24)
return header return header
} }
......
...@@ -41,7 +41,7 @@ class ViewController: UIViewController { ...@@ -41,7 +41,7 @@ class ViewController: UIViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
prepareView() prepareView()
// prepareAlignToParentHorizontallyAndVerticallyExample() prepareAlignToParentHorizontallyAndVerticallyExample()
prepareCenterExample() prepareCenterExample()
} }
...@@ -75,10 +75,13 @@ class ViewController: UIViewController { ...@@ -75,10 +75,13 @@ class ViewController: UIViewController {
let children: Array<UIView> = [label1, label2, label3, label4] let children: Array<UIView> = [label1, label2, label3, label4]
// Align the labels vertically with an equal height. // Align the labels vertically with an equal height.
view.layout.vertically(children, top: 100, bottom: 100) // view.layout.vertically(children, top: 100, bottom: 100)
// Align the labels horizontally with an equal width. // Align the labels horizontally with an equal width.
view.layout.horizontally(children, left: 30, right: 30, spacing: 30) // view.layout.horizontally(children, left: 30, right: 30, spacing: 30)
view.layout.horizontally(children, left: 30, right: 30, spacing: 30).vertically(children, top: 100, bottom: 100)
// Print out the dimensions of the labels. // Print out the dimensions of the labels.
for v in children { for v in children {
......
...@@ -233,7 +233,7 @@ public class BottomTabBar : UITabBar { ...@@ -233,7 +233,7 @@ public class BottomTabBar : UITabBar {
super.didMoveToSuperview() super.didMoveToSuperview()
if autoLayoutToSuperview { if autoLayoutToSuperview {
if let v: UIView = superview { if let v: UIView = superview {
v.layout.align.bottom(self) v.layout(self).bottom()
v.layout.horizontally(self) v.layout.horizontally(self)
} }
} }
......
...@@ -314,7 +314,7 @@ public class CardView : MaterialPulseView { ...@@ -314,7 +314,7 @@ public class CardView : MaterialPulseView {
h += "[\(k)]" h += "[\(k)]"
layout.align.bottom(b, bottom: contentInset.bottom + leftButtonsInset.bottom) layout.bottom(b, bottom: contentInset.bottom + leftButtonsInset.bottom)
i += 1 i += 1
} }
...@@ -343,7 +343,7 @@ public class CardView : MaterialPulseView { ...@@ -343,7 +343,7 @@ public class CardView : MaterialPulseView {
h += "-(right_left)-" h += "-(right_left)-"
} }
layout.align.bottom(b, bottom: contentInset.bottom + rightButtonsInset.bottom) layout.bottom(b, bottom: contentInset.bottom + rightButtonsInset.bottom)
i -= 1 i -= 1
} }
......
...@@ -378,7 +378,7 @@ public class ImageCardView : MaterialPulseView { ...@@ -378,7 +378,7 @@ public class ImageCardView : MaterialPulseView {
verticalFormat += "-[titleLabel]" verticalFormat += "-[titleLabel]"
views["titleLabel"] = v views["titleLabel"] = v
} else { } else {
layout.align.top(v, top: contentInset.top + titleLabelInset.top) layout.top(v, top: contentInset.top + titleLabelInset.top)
} }
layout.horizontally(v, left: contentInset.left + titleLabelInset.left, right: contentInset.right + titleLabelInset.right) layout.horizontally(v, left: contentInset.left + titleLabelInset.left, right: contentInset.right + titleLabelInset.right)
} }
...@@ -417,7 +417,7 @@ public class ImageCardView : MaterialPulseView { ...@@ -417,7 +417,7 @@ public class ImageCardView : MaterialPulseView {
h += "[\(k)]" h += "[\(k)]"
layout.align.bottom(b, bottom: contentInset.bottom + leftButtonsInset.bottom) layout.bottom(b, bottom: contentInset.bottom + leftButtonsInset.bottom)
i += 1 i += 1
} }
...@@ -446,7 +446,7 @@ public class ImageCardView : MaterialPulseView { ...@@ -446,7 +446,7 @@ public class ImageCardView : MaterialPulseView {
h += "-(right_left)-" h += "-(right_left)-"
} }
layout.align.bottom(b, bottom: contentInset.bottom + rightButtonsInset.bottom) layout.bottom(b, bottom: contentInset.bottom + rightButtonsInset.bottom)
i -= 1 i -= 1
} }
......
...@@ -30,159 +30,314 @@ ...@@ -30,159 +30,314 @@
import UIKit 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 {
Layout.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 {
Layout.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 {
Layout.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 {
Layout.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 {
Layout.alignFromBottomRight(v, child: child, bottom: bottom, right: right)
}
}
/// Top
public func top(child: UIView, top: CGFloat = 0) {
if let v: UIView = context {
Layout.alignFromTop(v, child: child, top: top)
}
}
/// Left
public func left(child: UIView, left: CGFloat = 0) {
if let v: UIView = context {
Layout.alignFromLeft(v, child: child, left: left)
}
}
/// Bottom
public func bottom(child: UIView, bottom: CGFloat = 0) {
if let v: UIView = context {
Layout.alignFromBottom(v, child: child, bottom: bottom)
}
}
/// Right
public func right(child: UIView, right: CGFloat = 0) {
if let v: UIView = context {
Layout.alignFromRight(v, child: child, right: right)
}
}
}
public class Layout { public class Layout {
/// Reference to Align. /// Reference to Align.
public private(set) var align: Align
/// UIView context. /// UIView context.
internal weak var context: UIView? internal weak var context: UIView?
/// Child UIView context.
internal weak var childContext: UIView?
init(context: UIView?) { init(context: UIView?) {
self.context = context self.context = context
self.align = Align(context: context)
} }
init(context: UIView?, childContext: UIView?) {
self.context = context
self.childContext = childContext
}
/// Width /// Width
public func width(child: UIView, width: CGFloat = 0) { public func width(child: UIView, width: CGFloat) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.width(v, child: child, width: width) Layout.width(v, child: child, width: width)
} }
return self
} }
/// Width (Assuming a child context)
public func width(width: CGFloat) -> Layout {
if let c: UIView = childContext {
self.width(c, width: width)
}
return self
}
/// Height /// Height
public func height(child: UIView, height: CGFloat = 0) { public func height(child: UIView, height: CGFloat) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.height(v, child: child, height: height) Layout.height(v, child: child, height: height)
} }
return self
} }
/// Height (Assuming a child context)
public func height(height: CGFloat) -> Layout {
if let c: UIView = childContext {
self.height(c, height: height)
}
return self
}
/// Size /// Size
public func size(child: UIView, width: CGFloat = 0, height: CGFloat = 0) { public func size(child: UIView, width: CGFloat, height: CGFloat) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.size(v, child: child, width: width, height: height) Layout.size(v, child: child, width: width, height: height)
} }
return self
} }
/// Size (Assuming a child context)
public func size(width: CGFloat, height: CGFloat) -> Layout {
if let c: UIView = childContext {
self.size(c, width: width, height: height)
}
return self
}
/// 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) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParentHorizontally(v, children: children, left: left, right: right, spacing: spacing) Layout.alignToParentHorizontally(v, children: children, left: left, right: right, spacing: spacing)
} }
return self
} }
/// 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) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParentVertically(v, children: children, top: top, bottom: bottom, spacing: spacing) Layout.alignToParentVertically(v, children: children, top: top, bottom: bottom, spacing: spacing)
} }
return self
} }
/// 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) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParentHorizontally(v, child: child, left: left, right: right) Layout.alignToParentHorizontally(v, child: child, left: left, right: right)
} }
return self
} }
/// Horizontally aligned (Assuming a child context)
public func horizontally(left: CGFloat = 0, right: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.horizontally(c, left: left, right: right)
}
return self
}
/// 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) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.alignToParentVertically(v, child: child, top: top, bottom: bottom) Layout.alignToParentVertically(v, child: child, top: top, bottom: bottom)
} }
return self
} }
/// Vertically aligned (Assuming a child context)
public func vertically(top: CGFloat = 0, bottom: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.vertically(c, top: top, bottom: bottom)
}
return self
}
/// Center /// Center
public func center(child: UIView, constantX: CGFloat = 0, constantY: CGFloat = 0) { public func center(child: UIView, constantX: CGFloat = 0, constantY: CGFloat = 0) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.center(v, child: child, constantX: constantX, constantY: constantY) Layout.center(v, child: child, constantX: constantX, constantY: constantY)
} }
} return self
}
/// CenterHorizontally
public func centerHorizontally(child: UIView, constant: CGFloat = 0) { /// Center (Assuming a child context)
public func center(constantX: CGFloat = 0, constantY: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.center(c, constantX: constantX, constantY: constantY)
}
return self
}
/// Center Horizontally
public func centerHorizontally(child: UIView, constant: CGFloat = 0) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.centerHorizontally(v, child: child, constant: constant) Layout.centerHorizontally(v, child: child, constant: constant)
} }
} return self
}
/// CenterVertically
public func centerVertically(child: UIView, constant: CGFloat = 0) { /// Center Horizontally (Assuming a child context)
public func centerHorizontally(constant: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.centerHorizontally(c, constant: constant)
}
return self
}
/// Center Vertically
public func centerVertically(child: UIView, constant: CGFloat = 0) -> Layout {
if let v: UIView = context { if let v: UIView = context {
Layout.centerVertically(v, child: child, constant: constant) Layout.centerVertically(v, child: child, constant: constant)
} }
} return self
}
/// Center Vertically (Assuming a child context)
public func centerVertically(constant: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.centerVertically(c, constant: constant)
}
return self
}
/// Align Edges
public func edges(child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignToParent(v, child: child, top: top, left: left, bottom: bottom, right: right)
}
return self
}
/// Align Edges (Assuming a child context)
public func edges(top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.edges(c, top: top, left: left, bottom: bottom, right: right)
}
return self
}
/// Align to TopLeft
public func topLeft(child: UIView, top: CGFloat = 0, left: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignFromTopLeft(v, child: child, top: top, left: left)
}
return self
}
/// Align to TopLeft (Assuming a child context)
public func topLeft(top: CGFloat = 0, left: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.topLeft(c, top: top, left: left)
}
return self
}
/// Align to TopRight
public func topRight(child: UIView, top: CGFloat = 0, right: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignFromTopRight(v, child: child, top: top, right: right)
}
return self
}
/// Align to TopRight (Assuming a child context)
public func topRight(top: CGFloat = 0, right: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.topRight(c, top: top, right: right)
}
return self
}
/// Align to BottomLeft
public func bottomLeft(child: UIView, bottom: CGFloat = 0, left: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignFromBottomLeft(v, child: child, bottom: bottom, left: left)
}
return self
}
/// Align to BottomLeft (Assuming a child context)
public func bottomLeft(bottom: CGFloat = 0, left: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.bottomLeft(c, bottom: bottom, left: left)
}
return self
}
/// Align to BottomRight
public func bottomRight(child: UIView, bottom: CGFloat = 0, right: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignFromBottomRight(v, child: child, bottom: bottom, right: right)
}
return self
}
/// Align to BottomRight (Assuming a child context)
public func bottomRight(bottom: CGFloat = 0, right: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.bottomRight(c, bottom: bottom, right: right)
}
return self
}
/// Align to Top
public func top(child: UIView, top: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignFromTop(v, child: child, top: top)
}
return self
}
/// Align to Top (Assuming a child context)
public func top(top: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.top(c, top: top)
}
return self
}
/// Align to Left
public func left(child: UIView, left: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignFromLeft(v, child: child, left: left)
}
return self
}
/// Align to Left (Assuming a child context)
public func left(left: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.left(c, left: left)
}
return self
}
/// Align to Bottom
public func bottom(child: UIView, bottom: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignFromBottom(v, child: child, bottom: bottom)
}
return self
}
/// Align to Bottom (Assuming a child context)
public func bottom(bottom: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.bottom(c, bottom: bottom)
}
return self
}
/// Align to Right
public func right(child: UIView, right: CGFloat = 0) -> Layout {
if let v: UIView = context {
Layout.alignFromRight(v, child: child, right: right)
}
return self
}
/// Align to Right (Assuming a child context)
public func right(right: CGFloat = 0) -> Layout {
if let c: UIView = childContext {
self.right(c, right: right)
}
return self
}
} }
/// Layout /// Layout
...@@ -364,4 +519,10 @@ public extension UIView { ...@@ -364,4 +519,10 @@ public extension UIView {
MaterialAssociateObject(self, key: &LayoutKey, value: value) MaterialAssociateObject(self, key: &LayoutKey, value: value)
} }
} }
/// Layout reference with a child context.
public func layout(child: UIView) -> Layout {
return Layout(context: self, childContext: child)
}
} }
...@@ -401,7 +401,7 @@ public class TextView: UITextView { ...@@ -401,7 +401,7 @@ public class TextView: UITextView {
internal func reloadView() { internal func reloadView() {
if let p = placeholderLabel { if let p = placeholderLabel {
removeConstraints(constraints) removeConstraints(constraints)
layout.align.edges(p, layout.edges(p,
top: textContainerInset.top, top: textContainerInset.top,
left: textContainerInset.left + textContainer.lineFragmentPadding, left: textContainerInset.left + textContainer.lineFragmentPadding,
bottom: textContainerInset.bottom, 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