diff --git a/KVConstraintKit/KVConstraintKit.swift b/KVConstraintKit/KVConstraintKit.swift index 48762f7..59c301d 100644 --- a/KVConstraintKit/KVConstraintKit.swift +++ b/KVConstraintKit/KVConstraintKit.swift @@ -37,7 +37,9 @@ public typealias View = NSView public typealias LayoutPriority = NSLayoutPriority - public typealias EdgeInsets = NSEdgeInsets + #if !swift(>=3.0) + public typealias EdgeInsets = NSEdgeInsets + #endif #endif extension EdgeInsets { @@ -106,7 +108,7 @@ extension View { #else switch attr1 { - case .Right, .Trailing, .Bottom: + case .right, .trailing, .bottom: return View.prepareConstraint(superview, attribute: attr1, secondView: self, attribute:attr2, relation: relation, multiplier:multiplier) default: break } diff --git a/KVConstraintKit/KVConstraintKitOperators.swift b/KVConstraintKit/KVConstraintKitOperators.swift index 1e71f0d..4c8d5c3 100644 --- a/KVConstraintKit/KVConstraintKitOperators.swift +++ b/KVConstraintKit/KVConstraintKitOperators.swift @@ -239,12 +239,24 @@ extension View : Modifiable { } /// (containerView ~ (constraint, multiplier)) public func *(lhs: View, rhs: (NSLayoutConstraint, CGFloat)) { - (lhs ~ (rhs.0, rhs.0.modified(multiplier: rhs.1))) + #if os(iOS) || os(tvOS) + let constraint = rhs.0.modified(multiplier: rhs.1) + #else + guard let constraint = rhs.0.modified(multiplier: rhs.1) else { return } + #endif + + (lhs ~ (rhs.0, constraint)) + } /// (containerView ~ (constraint, multiplier)) public func ~(lhs: View, rhs: (NSLayoutConstraint, NSLayoutRelation)) { - (lhs ~ (rhs.0, rhs.0.modified(relation: rhs.1))) + #if os(iOS) || os(tvOS) + let constraint = rhs.0.modified(relation: rhs.1) + #else + guard let constraint = rhs.0.modified(relation: rhs.1) else { return } + #endif + (lhs ~ (rhs.0, constraint)) } /// (containerView ~ (old, new)) diff --git a/KVConstraintKit/NSLayoutConstraintExtension.swift b/KVConstraintKit/NSLayoutConstraintExtension.swift index 730313b..eef0ad4 100644 --- a/KVConstraintKit/NSLayoutConstraintExtension.swift +++ b/KVConstraintKit/NSLayoutConstraintExtension.swift @@ -96,24 +96,48 @@ extension NSLayoutConstraint } +#if os(iOS) || os(tvOS) + public final func modified(relation r: NSLayoutRelation) -> NSLayoutConstraint { if relation == r { return self } - return NSLayoutConstraint(item: firstItem, attribute: firstAttribute, relatedBy: r, toItem: secondItem, attribute: secondAttribute, multiplier: multiplier, constant: constant) } - + public final func modified(multiplier m: CGFloat) -> NSLayoutConstraint { if multiplier == m { return self } - return NSLayoutConstraint(item: firstItem, attribute: firstAttribute, relatedBy: relation, toItem: secondItem, attribute: secondAttribute, multiplier: m, constant: constant) } +#else + + public final func modified(relation r: NSLayoutRelation) -> NSLayoutConstraint? + { + if relation == r { + return self + } + guard let firstItem = self.firstItem else { return nil } + return NSLayoutConstraint(item: firstItem, attribute: firstAttribute, relatedBy: r, toItem: secondItem, attribute: secondAttribute, multiplier: multiplier, constant: constant) + } + + public final func modified(multiplier m: CGFloat) -> NSLayoutConstraint? + { + if multiplier == m { + return self + } + guard let firstItem = self.firstItem else { return nil } + return NSLayoutConstraint(item: firstItem, attribute: firstAttribute, relatedBy: relation, toItem: secondItem, attribute: secondAttribute, multiplier: m, constant: constant) + } + +#endif + + + final func isSelfConstraint() -> Bool { // For aspect Ratio @@ -139,7 +163,7 @@ extension Array where Element: NSLayoutConstraint !( $0.firstItem is UILayoutSupport || $0.secondItem is UILayoutSupport) } #else - let reverseConstraints = reverse() + let reverseConstraints = reversed() #endif return reverseConstraints.filter {