Created
May 4, 2016 09:44
-
-
Save douglashill/008dc9197a2f32ba0cebc78f79d7f8c4 to your computer and use it in GitHub Desktop.
Turn layout constraints created with anchors into the old API so it works before iOS 9
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
// TODO: greater than, less than, multipliers, constants | |
import Foundation | |
let input = "[container.trailingAnchor constraintEqualToAnchor:childView.trailingAnchor]" | |
func attribute(fromAnchor anchor: String) -> String { | |
if let range = anchor.rangeOfString("Anchor") { | |
var s = anchor.substringToIndex(range.startIndex) | |
let firstChar = s.removeAtIndex(s.startIndex) | |
return "NSLayoutAttribute" + String(firstChar).uppercaseString + s | |
} | |
return "BAD" | |
} | |
let separators = NSCharacterSet(charactersInString: "[]. :") | |
let components = input.componentsSeparatedByCharactersInSet(separators).filter { $0 != "" } | |
components | |
let output = "[NSLayoutConstraint constraintWithItem:\(components[0]) attribute:\(attribute(fromAnchor: components[1])) relatedBy:NSLayoutRelationEqual toItem:\(components[3]) attribute:\(attribute(fromAnchor: components[4])) multiplier:1 constant:0]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment