Skip to content

Instantly share code, notes, and snippets.

@douglashill
Created May 4, 2016 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglashill/008dc9197a2f32ba0cebc78f79d7f8c4 to your computer and use it in GitHub Desktop.
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
// 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