2014-07-25 12:24:17 +08:00
|
|
|
//
|
2015-01-09 04:43:43 +08:00
|
|
|
// Snap
|
2014-07-25 12:24:17 +08:00
|
|
|
//
|
2014-07-29 08:39:59 +08:00
|
|
|
// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
|
2014-07-25 12:24:17 +08:00
|
|
|
//
|
2014-07-29 08:39:59 +08:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-30 08:55:31 +08:00
|
|
|
#if os(iOS)
|
2014-07-25 12:24:17 +08:00
|
|
|
import UIKit
|
2014-07-30 08:55:31 +08:00
|
|
|
#else
|
|
|
|
import AppKit
|
|
|
|
#endif
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
/**
|
|
|
|
* Constraint is a single item that defines all the properties for a single ConstraintMaker chain
|
|
|
|
*/
|
2015-04-11 13:13:02 +08:00
|
|
|
final public class Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
public var left: Constraint { return self.addConstraint(ConstraintAttributes.Left) }
|
|
|
|
public var top: Constraint { return self.addConstraint(ConstraintAttributes.Top) }
|
|
|
|
public var right: Constraint { return self.addConstraint(ConstraintAttributes.Right) }
|
|
|
|
public var bottom: Constraint { return self.addConstraint(ConstraintAttributes.Bottom) }
|
|
|
|
public var leading: Constraint { return self.addConstraint(ConstraintAttributes.Leading) }
|
|
|
|
public var trailing: Constraint { return self.addConstraint(ConstraintAttributes.Trailing) }
|
|
|
|
public var width: Constraint { return self.addConstraint(ConstraintAttributes.Width) }
|
|
|
|
public var height: Constraint { return self.addConstraint(ConstraintAttributes.Height) }
|
|
|
|
public var centerX: Constraint { return self.addConstraint(ConstraintAttributes.CenterX) }
|
|
|
|
public var centerY: Constraint { return self.addConstraint(ConstraintAttributes.CenterY) }
|
|
|
|
public var baseline: Constraint { return self.addConstraint(ConstraintAttributes.Baseline) }
|
2014-09-23 12:28:00 +08:00
|
|
|
|
2015-02-17 06:11:58 +08:00
|
|
|
public var and: Constraint {
|
|
|
|
if self.relation != nil {
|
|
|
|
fatalError("And is semantic only and can only be used before a relation is set.")
|
|
|
|
}
|
|
|
|
return self
|
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public var with: Constraint { return self }
|
2014-07-29 08:39:59 +08:00
|
|
|
|
|
|
|
// MARK: initializer
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
internal init(fromItem: ConstraintItem) {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.fromItem = fromItem
|
2015-01-08 09:58:12 +08:00
|
|
|
self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: equalTo
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
public func equalTo(other: ConstraintItem) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func equalTo(other: View) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-08 09:58:12 +08:00
|
|
|
#if os(iOS)
|
|
|
|
public func equalTo(other: UILayoutSupport) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2015-01-08 09:58:12 +08:00
|
|
|
}
|
|
|
|
#endif
|
2014-09-23 12:28:00 +08:00
|
|
|
public func equalTo(other: Float) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func equalTo(other: Double) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .Equal)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
|
|
|
public func equalTo(other: CGFloat) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .Equal)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func equalTo(other: Int) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func equalTo(other: UInt) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .Equal)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func equalTo(other: CGSize) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func equalTo(other: CGPoint) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func equalTo(other: EdgeInsets) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .Equal)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: lessThanOrEqualTo
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func lessThanOrEqualTo(other: View) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-08 09:58:12 +08:00
|
|
|
#if os(iOS)
|
|
|
|
public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2015-01-08 09:58:12 +08:00
|
|
|
}
|
|
|
|
#endif
|
2014-09-23 12:28:00 +08:00
|
|
|
public func lessThanOrEqualTo(other: Float) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func lessThanOrEqualTo(other: Double) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
|
|
|
public func lessThanOrEqualTo(other: CGFloat) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func lessThanOrEqualTo(other: Int) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func lessThanOrEqualTo(other: UInt) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func lessThanOrEqualTo(other: CGSize) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: greaterThanOrEqualTo
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-14 09:05:34 +08:00
|
|
|
public func greaterThanOrEqualTo(other: View) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-08 09:58:12 +08:00
|
|
|
#if os(iOS)
|
|
|
|
public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2015-01-08 09:58:12 +08:00
|
|
|
}
|
|
|
|
#endif
|
2014-09-23 12:28:00 +08:00
|
|
|
public func greaterThanOrEqualTo(other: Float) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func greaterThanOrEqualTo(other: Double) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
|
|
|
public func greaterThanOrEqualTo(other: CGFloat) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func greaterThanOrEqualTo(other: Int) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func greaterThanOrEqualTo(other: UInt) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
|
2015-02-17 06:11:58 +08:00
|
|
|
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: multiplier
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
public func multipliedBy(amount: Float) -> Constraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.multiplier = amount
|
|
|
|
return self
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func multipliedBy(amount: Double) -> Constraint {
|
|
|
|
return self.multipliedBy(Float(amount))
|
|
|
|
}
|
|
|
|
public func multipliedBy(amount: CGFloat) -> Constraint {
|
|
|
|
return self.multipliedBy(Float(amount))
|
|
|
|
}
|
|
|
|
public func multipliedBy(amount: Int) -> Constraint {
|
|
|
|
return self.multipliedBy(Float(amount))
|
|
|
|
}
|
|
|
|
public func multipliedBy(amount: UInt) -> Constraint {
|
|
|
|
return self.multipliedBy(Float(amount))
|
|
|
|
}
|
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
public func dividedBy(amount: Float) -> Constraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.multiplier = 1.0 / amount;
|
|
|
|
return self
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func dividedBy(amount: Double) -> Constraint {
|
|
|
|
return self.dividedBy(Float(amount))
|
|
|
|
}
|
|
|
|
public func dividedBy(amount: CGFloat) -> Constraint {
|
|
|
|
return self.dividedBy(Float(amount))
|
|
|
|
}
|
|
|
|
public func dividedBy(amount: Int) -> Constraint {
|
|
|
|
return self.dividedBy(Float(amount))
|
|
|
|
}
|
|
|
|
public func dividedBy(amount: UInt) -> Constraint {
|
|
|
|
return self.dividedBy(Float(amount))
|
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: priority
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
public func priority(priority: Float) -> Constraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.priority = priority
|
|
|
|
return self
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func priority(priority: Double) -> Constraint {
|
|
|
|
return self.priority(Float(priority))
|
|
|
|
}
|
|
|
|
public func priority(priority: CGFloat) -> Constraint {
|
|
|
|
return self.priority(Float(priority))
|
|
|
|
}
|
|
|
|
public func priority(priority: UInt) -> Constraint {
|
|
|
|
return self.priority(Float(priority))
|
|
|
|
}
|
|
|
|
public func priority(priority: Int) -> Constraint {
|
|
|
|
return self.priority(Float(priority))
|
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func priorityRequired() -> Constraint {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(1000.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func priorityHigh() -> Constraint {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(750.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func priorityLow() -> Constraint {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.priority(250.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// MARK: offset
|
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
public func offset(amount: Float) -> Constraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.offset = amount
|
|
|
|
return self
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
public func offset(amount: Double) -> Constraint {
|
|
|
|
return self.offset(Float(amount))
|
|
|
|
}
|
|
|
|
public func offset(amount: CGFloat) -> Constraint {
|
|
|
|
return self.offset(Float(amount))
|
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func offset(amount: Int) -> Constraint {
|
2014-10-03 09:59:47 +08:00
|
|
|
return self.offset(Float(amount))
|
|
|
|
}
|
|
|
|
public func offset(amount: UInt) -> Constraint {
|
|
|
|
return self.offset(Float(amount))
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func offset(amount: CGPoint) -> Constraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.offset = amount
|
|
|
|
return self
|
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func offset(amount: CGSize) -> Constraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.offset = amount
|
|
|
|
return self
|
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
public func offset(amount: EdgeInsets) -> Constraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.offset = amount
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: insets
|
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
public func insets(amount: EdgeInsets) -> Constraint {
|
2014-07-29 19:50:50 +08:00
|
|
|
self.offset = amount
|
2014-07-29 08:39:59 +08:00
|
|
|
return self
|
|
|
|
}
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-01-14 09:07:28 +08:00
|
|
|
// MARK: install / uninstall
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
public func install() -> [LayoutConstraint] {
|
2015-01-14 09:07:28 +08:00
|
|
|
return self.installOnView(updateExisting: false)
|
|
|
|
}
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-01-14 09:07:28 +08:00
|
|
|
public func uninstall() {
|
|
|
|
self.uninstallFromView()
|
|
|
|
}
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-01-08 14:04:29 +08:00
|
|
|
// MARK: internal
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
internal func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
|
2014-07-29 08:39:59 +08:00
|
|
|
var installOnView: View? = nil
|
2014-08-05 09:23:13 +08:00
|
|
|
if self.toItem.view != nil {
|
2014-07-29 08:39:59 +08:00
|
|
|
installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
|
2014-08-05 09:23:13 +08:00
|
|
|
if installOnView == nil {
|
2014-07-29 08:39:59 +08:00
|
|
|
NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
installOnView = self.fromItem.view?.superview
|
2014-08-05 09:23:13 +08:00
|
|
|
if installOnView == nil {
|
2015-01-26 20:27:14 +08:00
|
|
|
if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
|
|
|
|
installOnView = self.fromItem.view
|
|
|
|
}
|
|
|
|
|
2015-02-17 08:41:45 +08:00
|
|
|
if installOnView == nil {
|
2015-01-26 20:27:14 +08:00
|
|
|
NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
if let installedOnView = self.installInfo?.view {
|
|
|
|
if installedOnView != installOnView {
|
2015-02-17 08:41:45 +08:00
|
|
|
NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
|
|
|
|
return []
|
|
|
|
}
|
2015-04-11 13:41:05 +08:00
|
|
|
return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? []
|
2015-02-17 08:41:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
var newLayoutConstraints = [LayoutConstraint]()
|
2014-07-29 08:39:59 +08:00
|
|
|
let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
|
|
|
|
let layoutToAttributes = self.toItem.attributes.layoutAttributes
|
|
|
|
|
|
|
|
// get layout from
|
|
|
|
let layoutFrom: View? = self.fromItem.view
|
|
|
|
|
|
|
|
// get layout relation
|
2014-08-05 09:23:13 +08:00
|
|
|
let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
|
2014-07-29 08:39:59 +08:00
|
|
|
|
|
|
|
for layoutFromAttribute in layoutFromAttributes {
|
|
|
|
// get layout to attribute
|
|
|
|
let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
|
|
|
|
|
|
|
|
// get layout constant
|
|
|
|
var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
|
|
|
|
layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
|
|
|
|
|
2014-08-05 14:51:42 +08:00
|
|
|
// get layout to
|
|
|
|
var layoutTo: View? = self.toItem.view
|
|
|
|
if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
|
|
|
|
layoutTo = installOnView
|
|
|
|
}
|
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
// create layout constraint
|
|
|
|
let layoutConstraint = LayoutConstraint(
|
2014-09-23 12:28:00 +08:00
|
|
|
item: layoutFrom!,
|
2014-07-29 08:39:59 +08:00
|
|
|
attribute: layoutFromAttribute,
|
|
|
|
relatedBy: layoutRelation,
|
|
|
|
toItem: layoutTo,
|
|
|
|
attribute: layoutToAttribute,
|
|
|
|
multiplier: CGFloat(self.multiplier),
|
|
|
|
constant: layoutConstant)
|
|
|
|
|
|
|
|
// set priority
|
|
|
|
layoutConstraint.priority = self.priority
|
|
|
|
|
|
|
|
// set constraint
|
2015-04-11 13:41:05 +08:00
|
|
|
layoutConstraint.snp_constraint = self
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-02-17 06:11:58 +08:00
|
|
|
newLayoutConstraints.append(layoutConstraint)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
2015-01-08 14:20:34 +08:00
|
|
|
// special logic for updating
|
|
|
|
if updateExisting {
|
|
|
|
// get existing constraints for this view
|
|
|
|
let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
|
|
|
|
|
2015-02-17 06:11:58 +08:00
|
|
|
// array that will contain only new layout constraints to keep
|
2015-04-11 13:41:05 +08:00
|
|
|
var newLayoutConstraintsToKeep = [LayoutConstraint]()
|
2015-02-17 06:11:58 +08:00
|
|
|
|
2015-01-08 14:20:34 +08:00
|
|
|
// begin looping
|
2015-02-17 06:11:58 +08:00
|
|
|
for layoutConstraint in newLayoutConstraints {
|
2015-01-08 14:20:34 +08:00
|
|
|
// layout constraint that should be updated
|
|
|
|
var updateLayoutConstraint: LayoutConstraint? = nil
|
|
|
|
|
|
|
|
// loop through existing and check for match
|
|
|
|
for existingLayoutConstraint in existingLayoutConstraints {
|
|
|
|
if existingLayoutConstraint == layoutConstraint {
|
|
|
|
updateLayoutConstraint = existingLayoutConstraint
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have existing one lets just update the constant
|
|
|
|
if updateLayoutConstraint != nil {
|
|
|
|
updateLayoutConstraint!.constant = layoutConstraint.constant
|
|
|
|
}
|
2015-02-17 06:11:58 +08:00
|
|
|
// otherwise add this layout constraint to new keep list
|
2015-01-08 14:20:34 +08:00
|
|
|
else {
|
2015-02-17 06:11:58 +08:00
|
|
|
newLayoutConstraintsToKeep.append(layoutConstraint)
|
2015-01-08 14:20:34 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 06:11:58 +08:00
|
|
|
|
2015-01-08 14:20:34 +08:00
|
|
|
// set constraints to only new ones
|
2015-02-17 06:11:58 +08:00
|
|
|
newLayoutConstraints = newLayoutConstraintsToKeep
|
2015-01-08 14:20:34 +08:00
|
|
|
}
|
|
|
|
|
2015-01-14 09:16:26 +08:00
|
|
|
// add constraints
|
2015-02-17 06:11:58 +08:00
|
|
|
installOnView!.addConstraints(newLayoutConstraints)
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
// set install info
|
|
|
|
self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
|
2015-02-17 06:11:58 +08:00
|
|
|
|
|
|
|
// store which layout constraints are installed for this constraint
|
|
|
|
for layoutConstraint in newLayoutConstraints {
|
2015-04-11 13:41:05 +08:00
|
|
|
self.installInfo!.layoutConstraints.addObject(layoutConstraint)
|
2015-01-26 20:27:14 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 14:32:33 +08:00
|
|
|
// store the layout constraints against the layout from view
|
2015-04-11 13:41:05 +08:00
|
|
|
layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
|
2015-02-17 06:11:58 +08:00
|
|
|
|
|
|
|
// return the new constraints
|
|
|
|
return newLayoutConstraints
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
2015-01-14 09:07:28 +08:00
|
|
|
internal func uninstallFromView() {
|
2015-04-11 13:41:05 +08:00
|
|
|
if let installInfo = self.installInfo,
|
|
|
|
let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
|
2015-01-26 20:27:14 +08:00
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
if installedLayoutConstraints.count > 0 {
|
|
|
|
|
2015-04-11 14:32:33 +08:00
|
|
|
if let installedOnView = installInfo.view {
|
|
|
|
// remove the constraints from the UIView's storage
|
|
|
|
installedOnView.removeConstraints(installedLayoutConstraints)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the constraints from the from item view
|
|
|
|
if let fromView = self.fromItem.view {
|
|
|
|
fromView.snp_installedLayoutConstraints = fromView.snp_installedLayoutConstraints.filter {
|
|
|
|
return !contains(installedLayoutConstraints, $0)
|
|
|
|
}
|
2015-01-14 09:16:26 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 13:41:05 +08:00
|
|
|
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 13:41:05 +08:00
|
|
|
self.installInfo = nil
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: private
|
|
|
|
|
|
|
|
private let fromItem: ConstraintItem
|
|
|
|
private var toItem: ConstraintItem
|
|
|
|
private var relation: ConstraintRelation?
|
2015-04-11 13:41:05 +08:00
|
|
|
private var constant: Any = Float(0.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
private var multiplier: Float = 1.0
|
|
|
|
private var priority: Float = 1000.0
|
2015-04-11 13:41:05 +08:00
|
|
|
private var offset: Any = Float(0.0)
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
private var installInfo: ConstraintInstallInfo?
|
2014-07-29 08:39:59 +08:00
|
|
|
|
|
|
|
private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
|
2014-08-05 09:23:13 +08:00
|
|
|
if self.relation == nil {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.fromItem.attributes += attributes
|
|
|
|
}
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
|
|
|
|
if other.attributes != ConstraintAttributes.None {
|
2015-04-11 12:11:20 +08:00
|
|
|
let toLayoutAttributes = other.attributes.layoutAttributes
|
2014-07-29 08:39:59 +08:00
|
|
|
if toLayoutAttributes.count > 1 {
|
2015-04-11 12:11:20 +08:00
|
|
|
let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
|
2014-07-29 08:39:59 +08:00
|
|
|
if toLayoutAttributes != fromLayoutAttributes {
|
|
|
|
NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
other.attributes = ConstraintAttributes.None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.toItem = other
|
|
|
|
self.relation = relation
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint {
|
|
|
|
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-01-08 09:58:12 +08:00
|
|
|
#endif
|
2014-07-29 08:39:59 +08:00
|
|
|
private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
|
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
|
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-10-03 09:59:47 +08:00
|
|
|
}
|
2014-07-29 08:39:59 +08:00
|
|
|
private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
|
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
|
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2014-07-30 08:55:31 +08:00
|
|
|
private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
|
2014-07-29 08:39:59 +08:00
|
|
|
self.constant = other
|
2015-01-08 09:58:12 +08:00
|
|
|
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
|
2015-04-11 12:11:20 +08:00
|
|
|
var views = Set<View>()
|
2015-02-17 06:37:43 +08:00
|
|
|
var fromView = fromView
|
|
|
|
var toView = toView
|
|
|
|
do {
|
|
|
|
if let view = toView {
|
2015-04-11 12:11:20 +08:00
|
|
|
if views.contains(view) {
|
2015-02-17 06:37:43 +08:00
|
|
|
return view
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-04-11 12:11:20 +08:00
|
|
|
views.insert(view)
|
2015-02-17 06:37:43 +08:00
|
|
|
toView = view.superview
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
2015-02-17 06:37:43 +08:00
|
|
|
if let view = fromView {
|
2015-04-11 12:11:20 +08:00
|
|
|
if views.contains(view) {
|
2015-02-17 06:37:43 +08:00
|
|
|
return view
|
|
|
|
}
|
2015-04-11 12:11:20 +08:00
|
|
|
views.insert(view)
|
2015-02-17 06:37:43 +08:00
|
|
|
fromView = view.superview
|
|
|
|
}
|
|
|
|
} while (fromView != nil || toView != nil)
|
|
|
|
|
|
|
|
return nil
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension NSLayoutAttribute {
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
private func snp_offsetForValue(value: Any?) -> CGFloat {
|
2014-07-29 08:39:59 +08:00
|
|
|
// Float
|
|
|
|
if let float = value as? Float {
|
|
|
|
return CGFloat(float)
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// Double
|
|
|
|
else if let double = value as? Double {
|
|
|
|
return CGFloat(double)
|
|
|
|
}
|
|
|
|
// UInt
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let int = value as? Int {
|
|
|
|
return CGFloat(int)
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// Int
|
|
|
|
else if let uint = value as? UInt {
|
|
|
|
return CGFloat(uint)
|
|
|
|
}
|
|
|
|
// CGFloat
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let float = value as? CGFloat {
|
|
|
|
return float
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// CGSize
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let size = value as? CGSize {
|
|
|
|
if self == .Width {
|
|
|
|
return size.width
|
|
|
|
} else if self == .Height {
|
|
|
|
return size.height
|
|
|
|
}
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// CGPoint
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let point = value as? CGPoint {
|
|
|
|
if self == .Left || self == .CenterX {
|
|
|
|
return point.x
|
|
|
|
} else if self == .Top || self == .CenterY {
|
|
|
|
return point.y
|
|
|
|
} else if self == .Right {
|
|
|
|
return -point.x
|
|
|
|
} else if self == .Bottom {
|
|
|
|
return -point.y
|
|
|
|
}
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// EdgeInsets
|
2014-07-30 08:55:31 +08:00
|
|
|
else if let insets = value as? EdgeInsets {
|
2014-07-29 08:39:59 +08:00
|
|
|
if self == .Left {
|
|
|
|
return insets.left
|
|
|
|
} else if self == .Top {
|
|
|
|
return insets.top
|
|
|
|
} else if self == .Right {
|
|
|
|
return -insets.right
|
|
|
|
} else if self == .Bottom {
|
|
|
|
return -insets.bottom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CGFloat(0)
|
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2014-09-23 12:28:00 +08:00
|
|
|
private func snp_constantForValue(value: Any?) -> CGFloat {
|
2014-07-29 08:39:59 +08:00
|
|
|
// Float
|
|
|
|
if let float = value as? Float {
|
|
|
|
return CGFloat(float)
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// Double
|
|
|
|
else if let double = value as? Double {
|
|
|
|
return CGFloat(double)
|
|
|
|
}
|
|
|
|
// UInt
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let int = value as? Int {
|
|
|
|
return CGFloat(int)
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// Int
|
|
|
|
else if let uint = value as? UInt {
|
|
|
|
return CGFloat(uint)
|
|
|
|
}
|
|
|
|
// CGFloat
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let float = value as? CGFloat {
|
|
|
|
return float
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// CGSize
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let size = value as? CGSize {
|
|
|
|
if self == .Width {
|
|
|
|
return size.width
|
|
|
|
} else if self == .Height {
|
|
|
|
return size.height
|
|
|
|
}
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// CGPoint
|
2014-07-29 08:39:59 +08:00
|
|
|
else if let point = value as? CGPoint {
|
|
|
|
if self == .Left || self == .CenterX {
|
|
|
|
return point.x
|
|
|
|
} else if self == .Top || self == .CenterY {
|
|
|
|
return point.y
|
|
|
|
} else if self == .Right {
|
|
|
|
return point.x
|
|
|
|
} else if self == .Bottom {
|
|
|
|
return point.y
|
|
|
|
}
|
|
|
|
}
|
2014-10-03 09:59:47 +08:00
|
|
|
// EdgeInsets
|
2014-07-30 08:55:31 +08:00
|
|
|
else if let insets = value as? EdgeInsets {
|
2014-07-29 08:39:59 +08:00
|
|
|
if self == .Left {
|
|
|
|
return insets.left
|
|
|
|
} else if self == .Top {
|
|
|
|
return insets.top
|
|
|
|
} else if self == .Right {
|
2014-10-29 06:45:56 +08:00
|
|
|
return -insets.right
|
2014-07-29 08:39:59 +08:00
|
|
|
} else if self == .Bottom {
|
2014-10-29 06:45:56 +08:00
|
|
|
return -insets.bottom
|
2014-07-29 08:39:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CGFloat(0);
|
|
|
|
}
|
2015-01-08 14:04:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 13:41:05 +08:00
|
|
|
private struct ConstraintInstallInfo {
|
|
|
|
|
|
|
|
weak var view: UIView? = nil
|
|
|
|
let layoutConstraints: NSHashTable
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-08 14:04:29 +08:00
|
|
|
internal func ==(left: Constraint, right: Constraint) -> Bool {
|
|
|
|
return (left.fromItem == right.fromItem &&
|
|
|
|
left.toItem == right.toItem &&
|
|
|
|
left.relation == right.relation &&
|
|
|
|
left.multiplier == right.multiplier &&
|
|
|
|
left.priority == right.priority)
|
2014-09-23 12:28:00 +08:00
|
|
|
}
|