Skip to content

Instantly share code, notes, and snippets.

@UjwalManjunath
Created October 3, 2016 02:06
Show Gist options
  • Save UjwalManjunath/abf07319054ee42d3a178e8f72634750 to your computer and use it in GitHub Desktop.
Save UjwalManjunath/abf07319054ee42d3a178e8f72634750 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
extension UIView {
func constraintForAttribute(attribute:NSLayoutAttribute) -> NSLayoutConstraint? {
for constraint in self.constraints {
if constraint.firstAttribute == attribute {
return constraint
}
}
return nil
}
func constraintWithView(view:UIView, attribute:NSLayoutAttribute) -> NSLayoutConstraint? {
for constraint in self.constraints {
//Guard for the constraints concerning the views we care about
guard let secondItem = constraint.secondItem as? UIView else { continue }
guard secondItem == view || constraint.firstItem as! UIView == view else { continue }
if (constraint.firstAttribute == attribute && constraint.firstItem as! UIView == view) {
return constraint
} else if (secondItem == view && constraint.secondAttribute == attribute) {
return constraint
}
}
if let cell = self as? UICollectionViewCell {
return cell.contentView.constraintWithView(view, attribute: attribute)
}
print("Unable to find constraint for attribute: \(attribute)")
return nil
}
public class func instantiateFromNib<T: UIView>(viewType: T.Type) -> T {
let url = NSURL(string: NSStringFromClass(viewType))
return NSBundle.mainBundle().loadNibNamed(url?.pathExtension, owner: nil, options: nil).first as! T
}
public class func instantiateFromNib() -> Self {
return instantiateFromNib(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment