Commit 4af601ee by M. Porooshani

Added centering methods to MaterialLayout with examples

parent 9c040edc
...@@ -43,6 +43,7 @@ class ViewController: UIViewController { ...@@ -43,6 +43,7 @@ class ViewController: UIViewController {
prepareView() prepareView()
prepareAlignToParentHorizontallyExample() prepareAlignToParentHorizontallyExample()
prepareAlignToParentVerticallyExample() prepareAlignToParentVerticallyExample()
prepareCenterExample()
} }
/// General preparation statements. /// General preparation statements.
...@@ -141,5 +142,49 @@ class ViewController: UIViewController { ...@@ -141,5 +142,49 @@ class ViewController: UIViewController {
print(v.frame) print(v.frame)
} }
} }
// Lays out test views to the center of different axes
private func prepareCenterExample() {
let length: CGFloat = 100
let labelCX = UILabel()
labelCX.backgroundColor = MaterialColor.grey.base
labelCX.text = "centerX"
labelCX.textAlignment = .Center
labelCX.layer.cornerRadius = length / 2.0
labelCX.clipsToBounds = true
view.addSubview(labelCX)
MaterialLayout.width(view, child: labelCX, width: length)
MaterialLayout.height(view, child: labelCX, height: length)
MaterialLayout.centerHorizontally(view, child: labelCX)
let labelCY = UILabel()
labelCY.backgroundColor = MaterialColor.grey.base
labelCY.text = "centerY"
labelCY.textAlignment = .Center
labelCY.layer.cornerRadius = length / 2.0
labelCY.clipsToBounds = true
view.addSubview(labelCY)
MaterialLayout.width(view, child: labelCY, width: length)
MaterialLayout.height(view, child: labelCY, height: length)
MaterialLayout.centerVertically(view, child: labelCY)
let labelCXY = UILabel()
labelCXY.backgroundColor = MaterialColor.grey.base
labelCXY.text = "centerXY"
labelCXY.textAlignment = .Center
labelCXY.layer.cornerRadius = length / 2.0
labelCXY.clipsToBounds = true
view.addSubview(labelCXY)
MaterialLayout.width(view, child: labelCXY, width: length)
MaterialLayout.height(view, child: labelCXY, height: length)
MaterialLayout.center(view, child: labelCXY)
}
} }
...@@ -147,6 +147,23 @@ public struct MaterialLayout { ...@@ -147,6 +147,23 @@ public struct MaterialLayout {
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1.0, constant: -right)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .Right, relatedBy: .Equal, toItem: parent, attribute: .Right, multiplier: 1.0, constant: -right))
} }
/// CenterHorizontally
public static func centerHorizontally(parent: UIView, child: UIView, constant: CGFloat = 0) {
prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .CenterX, relatedBy: .Equal, toItem: parent, attribute: .CenterX, multiplier: 1.0, constant: constant))
}
/// CenterVertically
public static func centerVertically(parent: UIView, child: UIView, constant: CGFloat = 0) {
prepareForConstraint([child])
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .CenterY, relatedBy: .Equal, toItem: parent, attribute: .CenterY, multiplier: 1.0, constant: constant))
}
/// 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)
}
/// prepareForConstraint /// prepareForConstraint
private static func prepareForConstraint(views: [UIView]) { private static func prepareForConstraint(views: [UIView]) {
......
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