Created
March 16, 2016 20:31
-
-
Save alsedi/dfcaed5e20da361d82b0 to your computer and use it in GitHub Desktop.
Easy access to constraints by Identifier
This file contains 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
// | |
// UIView+Contstraints.swift | |
// | |
// HOW TO USE | |
// 1. Setup constraints and define NSLayoutConstraint.identifier | |
// 2. Use identifier as parameter for call | |
// 3. Call on superview: if let constraint = self.view.constraintByStringId("Id") { } | |
// 4. Change, animate or replace constraint | |
import Foundation | |
import UIKit | |
extension UIView { | |
func constraintByStringId(stringId: String) -> NSLayoutConstraint? { | |
for constraint in self.constraints { | |
if constraint.identifier == stringId { | |
return constraint | |
} | |
} | |
return nil | |
} | |
func constraintsByStringId(stringId: String) -> [NSLayoutConstraint]? { | |
var result = [NSLayoutConstraint]() | |
for constraint in self.constraints { | |
if constraint.identifier == stringId { | |
result.append(constraint) | |
} | |
} | |
return result.count == 0 ? nil : result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment