Last active
August 29, 2015 14:02
-
-
Save ssaluk/55d4caed2220c8415a06 to your computer and use it in GitHub Desktop.
Swift operators overloading and autolayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LayoutConstraint.swift | |
// SwiftControls | |
// | |
import UIKit | |
struct LayoutAttribute | |
{ | |
var view:UIView? | |
var attribute:NSLayoutAttribute | |
var multiplier:CGFloat | |
var constant:CGFloat | |
init(view:UIView, attribute: NSLayoutAttribute, constant:CGFloat = 0, multiplier:CGFloat = 1.0){ | |
self.view = view; | |
self.attribute = attribute | |
self.multiplier = multiplier | |
self.constant = constant; | |
} | |
init(_ constant:CGFloat){ | |
self.attribute = .NotAnAttribute | |
self.multiplier = 1 | |
self.constant = constant; | |
} | |
} | |
func == (left: LayoutAttribute, right: LayoutAttribute) -> NSLayoutConstraint { | |
var layoutConstraint = NSLayoutConstraint(item:left.view!, | |
attribute: left.attribute, relatedBy: NSLayoutRelation.Equal, toItem: right.view, | |
attribute: right.attribute, multiplier: right.multiplier, constant: right.constant) | |
return layoutConstraint | |
} | |
func >= (left: LayoutAttribute, right: LayoutAttribute) -> NSLayoutConstraint { | |
var layoutConstraint = NSLayoutConstraint(item: left.view!, | |
attribute: left.attribute, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: right.view, attribute: right.attribute, | |
multiplier: right.multiplier, constant: right.constant); | |
return layoutConstraint | |
} | |
func <= (left: LayoutAttribute, right: LayoutAttribute) -> NSLayoutConstraint { | |
var layoutConstraint = NSLayoutConstraint(item: left.view!, | |
attribute: left.attribute, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: right.view, attribute: right.attribute, | |
multiplier: right.multiplier, constant: right.constant); | |
return layoutConstraint | |
} | |
func == (left: LayoutAttribute, right: CGFloat) -> NSLayoutConstraint { | |
var layoutConstraint = NSLayoutConstraint(item:left.view!, | |
attribute: left.attribute, relatedBy: NSLayoutRelation.Equal, toItem: nil, | |
attribute: .NotAnAttribute, multiplier: 1.0, constant: right) | |
return layoutConstraint | |
} | |
func <= (left: LayoutAttribute, right: CGFloat) -> NSLayoutConstraint { | |
var layoutConstraint = NSLayoutConstraint(item:left.view!, | |
attribute: left.attribute, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: nil, | |
attribute: .NotAnAttribute, multiplier: 1.0, constant: right) | |
return layoutConstraint | |
} | |
func >= (left: LayoutAttribute, right: CGFloat) -> NSLayoutConstraint { | |
var layoutConstraint = NSLayoutConstraint(item:left.view!, | |
attribute: left.attribute, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil, | |
attribute: .NotAnAttribute, multiplier: 1.0, constant: right) | |
return layoutConstraint | |
} | |
func *(left: LayoutAttribute, right: (CGFloat, CGFloat)) -> LayoutAttribute { | |
var layoutAttribute = LayoutAttribute(view:left.view!, attribute: left.attribute, constant: right.0, multiplier:right.1) | |
return layoutAttribute | |
} | |
func +(left: LayoutAttribute, right: CGFloat) -> LayoutAttribute { | |
var layoutAttribute = LayoutAttribute(view:left.view!, attribute: left.attribute, constant: right, multiplier:1.0) | |
return layoutAttribute | |
} | |
func -(left: LayoutAttribute, right: CGFloat) -> LayoutAttribute { | |
var layoutAttribute = LayoutAttribute(view:left.view!, attribute: left.attribute, constant: -right, multiplier:1.0) | |
return layoutAttribute | |
} | |
extension UIView | |
{ | |
func addConstraints (constraints: NSLayoutConstraint...){ | |
for constraint in constraints { | |
self.addConstraint(constraint) | |
} | |
} | |
var width : LayoutAttribute { | |
get { return LayoutAttribute(view: self, attribute : .Width); } | |
} | |
var height : LayoutAttribute { | |
get { return LayoutAttribute(view: self, attribute : .Height); } | |
} | |
var leading : LayoutAttribute { | |
get { return LayoutAttribute(view: self, attribute : .Leading); } | |
} | |
var trailing : LayoutAttribute { | |
get { return LayoutAttribute(view: self, attribute : .Trailing); } | |
} | |
var top : LayoutAttribute { | |
get { return LayoutAttribute(view: self, attribute : .Top); } | |
} | |
var bottom : LayoutAttribute { | |
get { return LayoutAttribute(view: self, attribute : .Bottom); } | |
} | |
var centerX : LayoutAttribute { | |
get { return LayoutAttribute(view: self, attribute : .CenterX); } | |
} | |
var centerY : LayoutAttribute { | |
get { return LayoutAttribute(view: self, attribute : .CenterY); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment