Commit 8e91d135 by Daniel Dahan

cleanup internally in Layout class

parent a6f0225a
......@@ -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
extension Layout {
/**
......@@ -640,9 +653,7 @@ extension Layout {
public class 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))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -654,9 +665,7 @@ extension Layout {
public class 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))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -681,6 +690,7 @@ extension Layout {
*/
public class func horizontally(parent: UIView, children: [UIView], left: CGFloat = 0, right: CGFloat = 0, interimSpace: InterimSpace = 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 {
......@@ -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))
}
for child in children {
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
}
......@@ -718,9 +727,7 @@ extension Layout {
}
for child in children {
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
}
......@@ -733,13 +740,9 @@ extension Layout {
*/
public class func horizontally(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))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -751,13 +754,9 @@ extension Layout {
*/
public class func vertically(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))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -783,12 +782,8 @@ extension Layout {
*/
public class func top(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))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -800,12 +795,8 @@ extension Layout {
*/
public class func left(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))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -817,12 +808,8 @@ extension Layout {
*/
public class func bottom(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))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -834,12 +821,8 @@ extension Layout {
*/
public class func right(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))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -916,12 +899,8 @@ extension Layout {
*/
public class func centerHorizontally(parent: UIView, child: UIView, offset: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: 1, constant: offset))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......@@ -933,12 +912,8 @@ extension Layout {
*/
public class func centerVertically(parent: UIView, child: UIView, offset: CGFloat = 0) {
prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: 1, constant: offset))
child.updateConstraints()
child.setNeedsLayout()
child.layoutIfNeeded()
updateConstraints(for: child)
}
/**
......
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