Commit 8e91d135 by Daniel Dahan

cleanup internally in Layout class

parent a6f0225a
...@@ -629,6 +629,19 @@ public class Layout { ...@@ -629,6 +629,19 @@ public class Layout {
} }
} }
fileprivate extension Layout {
/**
Updates and lays out the constraints for a given view.
- Parameter for view: A UIView.
*/
class func updateConstraints(for view: UIView) {
view.updateConstraintsIfNeeded()
view.updateConstraints()
view.setNeedsLayout()
view.layoutIfNeeded()
}
}
/// Layout /// Layout
extension Layout { extension Layout {
/** /**
...@@ -640,9 +653,7 @@ extension Layout { ...@@ -640,9 +653,7 @@ extension Layout {
public class func width(parent: UIView, child: UIView, width: CGFloat = 0) { public class func width(parent: UIView, child: UIView, width: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: width)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: width))
child.updateConstraints() updateConstraints(for: child)
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -654,9 +665,7 @@ extension Layout { ...@@ -654,9 +665,7 @@ extension Layout {
public class func height(parent: UIView, child: UIView, height: CGFloat = 0) { public class func height(parent: UIView, child: UIView, height: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: height)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: height))
child.updateConstraints() updateConstraints(for: child)
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -681,6 +690,7 @@ extension Layout { ...@@ -681,6 +690,7 @@ extension Layout {
*/ */
public class func horizontally(parent: UIView, children: [UIView], left: CGFloat = 0, right: CGFloat = 0, interimSpace: InterimSpace = 0) { public class func horizontally(parent: UIView, children: [UIView], left: CGFloat = 0, right: CGFloat = 0, interimSpace: InterimSpace = 0) {
prepareForConstraint(parent, children: children) prepareForConstraint(parent, children: children)
if 0 < children.count { if 0 < children.count {
parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left)) parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left))
for i in 1..<children.count { for i in 1..<children.count {
...@@ -689,10 +699,9 @@ extension Layout { ...@@ -689,10 +699,9 @@ extension Layout {
} }
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right)) parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
} }
for child in children { for child in children {
child.updateConstraints() updateConstraints(for: child)
child.setNeedsLayout()
child.layoutIfNeeded()
} }
} }
...@@ -718,9 +727,7 @@ extension Layout { ...@@ -718,9 +727,7 @@ extension Layout {
} }
for child in children { for child in children {
child.updateConstraints() updateConstraints(for: child)
child.setNeedsLayout()
child.layoutIfNeeded()
} }
} }
...@@ -733,13 +740,9 @@ extension Layout { ...@@ -733,13 +740,9 @@ extension Layout {
*/ */
public class func horizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0) { public class func horizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0) {
prepareForConstraint(parent, child: child) 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: .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)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
updateConstraints(for: child)
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -751,13 +754,9 @@ extension Layout { ...@@ -751,13 +754,9 @@ extension Layout {
*/ */
public class func vertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) { public class func vertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) {
prepareForConstraint(parent, child: child) 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: .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)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom))
updateConstraints(for: child)
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -783,12 +782,8 @@ extension Layout { ...@@ -783,12 +782,8 @@ extension Layout {
*/ */
public class func top(parent: UIView, child: UIView, top: CGFloat = 0) { public class func top(parent: UIView, child: UIView, top: CGFloat = 0) {
prepareForConstraint(parent, child: child) 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: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1, constant: top))
updateConstraints(for: child)
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -800,12 +795,8 @@ extension Layout { ...@@ -800,12 +795,8 @@ extension Layout {
*/ */
public class func left(parent: UIView, child: UIView, left: CGFloat = 0) { public class func left(parent: UIView, child: UIView, left: CGFloat = 0) {
prepareForConstraint(parent, child: child) 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: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left))
updateConstraints(for: child)
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -817,12 +808,8 @@ extension Layout { ...@@ -817,12 +808,8 @@ extension Layout {
*/ */
public class func bottom(parent: UIView, child: UIView, bottom: CGFloat = 0) { public class func bottom(parent: UIView, child: UIView, bottom: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom))
updateConstraints(for: child)
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -834,12 +821,8 @@ extension Layout { ...@@ -834,12 +821,8 @@ extension Layout {
*/ */
public class func right(parent: UIView, child: UIView, right: CGFloat = 0) { public class func right(parent: UIView, child: UIView, right: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
updateConstraints(for: child)
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -916,12 +899,8 @@ extension Layout { ...@@ -916,12 +899,8 @@ extension Layout {
*/ */
public class func centerHorizontally(parent: UIView, child: UIView, offset: CGFloat = 0) { public class func centerHorizontally(parent: UIView, child: UIView, offset: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: 1, constant: offset)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: 1, constant: offset))
updateConstraints(for: child)
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -933,12 +912,8 @@ extension Layout { ...@@ -933,12 +912,8 @@ extension Layout {
*/ */
public class func centerVertically(parent: UIView, child: UIView, offset: CGFloat = 0) { public class func centerVertically(parent: UIView, child: UIView, offset: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: 1, constant: offset)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: 1, constant: offset))
updateConstraints(for: child)
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
......
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