2014-07-25 12:24:17 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// SnapKit
|
2014-07-25 12:24:17 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
|
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
|
|
|
|
2015-09-25 12:37:33 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2016-01-26 18:49:04 +08:00
|
|
|
import UIKit
|
2014-07-30 08:55:31 +08:00
|
|
|
#else
|
2016-01-26 18:49:04 +08:00
|
|
|
import AppKit
|
2014-07-30 08:55:31 +08:00
|
|
|
#endif
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-15 19:31:03 +08:00
|
|
|
public class Constraint {
|
2015-04-11 19:39:12 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal let sourceLocation: (String, UInt)
|
2016-08-06 18:38:02 +08:00
|
|
|
internal let label: String?
|
2015-04-12 19:32:03 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
private let from: ConstraintItem
|
|
|
|
private let to: ConstraintItem
|
|
|
|
private let relation: ConstraintRelation
|
|
|
|
private let multiplier: ConstraintMultiplierTarget
|
2016-08-04 06:46:25 +08:00
|
|
|
private var constant: ConstraintConstantTarget {
|
|
|
|
didSet {
|
|
|
|
self.updateConstantAndPriorityIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private var priority: ConstraintPriorityTarget {
|
|
|
|
didSet {
|
|
|
|
self.updateConstantAndPriorityIfNeeded()
|
|
|
|
}
|
|
|
|
}
|
2016-09-07 16:23:54 +08:00
|
|
|
private var layoutConstraints: [LayoutConstraint]
|
2015-04-12 19:32:03 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
// MARK: Initialization
|
2015-04-12 19:32:03 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal init(from: ConstraintItem,
|
|
|
|
to: ConstraintItem,
|
|
|
|
relation: ConstraintRelation,
|
|
|
|
sourceLocation: (String, UInt),
|
2016-08-06 18:38:02 +08:00
|
|
|
label: String?,
|
2016-01-26 18:49:04 +08:00
|
|
|
multiplier: ConstraintMultiplierTarget,
|
|
|
|
constant: ConstraintConstantTarget,
|
|
|
|
priority: ConstraintPriorityTarget) {
|
|
|
|
self.from = from
|
|
|
|
self.to = to
|
|
|
|
self.relation = relation
|
|
|
|
self.sourceLocation = sourceLocation
|
2016-08-06 18:38:02 +08:00
|
|
|
self.label = label
|
2016-01-26 18:49:04 +08:00
|
|
|
self.multiplier = multiplier
|
|
|
|
self.constant = constant
|
|
|
|
self.priority = priority
|
2016-09-07 16:23:54 +08:00
|
|
|
self.layoutConstraints = []
|
2016-08-06 18:38:02 +08:00
|
|
|
|
|
|
|
// get attributes
|
|
|
|
let layoutFromAttributes = self.from.attributes.layoutAttributes
|
|
|
|
let layoutToAttributes = self.to.attributes.layoutAttributes
|
|
|
|
|
|
|
|
// get layout from
|
|
|
|
let layoutFrom: ConstraintView = self.from.view!
|
|
|
|
|
|
|
|
// get relation
|
|
|
|
let layoutRelation = self.relation.layoutRelation
|
|
|
|
|
|
|
|
for layoutFromAttribute in layoutFromAttributes {
|
|
|
|
// get layout to attribute
|
2016-09-01 21:23:25 +08:00
|
|
|
let layoutToAttribute: NSLayoutAttribute
|
2016-09-01 21:39:08 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2016-09-10 12:38:40 +08:00
|
|
|
if layoutToAttributes.count > 0 {
|
2016-09-01 21:39:08 +08:00
|
|
|
if self.from.attributes == .edges && self.to.attributes == .margins {
|
|
|
|
switch layoutFromAttribute {
|
|
|
|
case .left:
|
|
|
|
layoutToAttribute = .leftMargin
|
|
|
|
case .right:
|
|
|
|
layoutToAttribute = .rightMargin
|
|
|
|
case .top:
|
|
|
|
layoutToAttribute = .topMargin
|
|
|
|
case .bottom:
|
|
|
|
layoutToAttribute = .bottomMargin
|
|
|
|
default:
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
} else if self.from.attributes == .margins && self.to.attributes == .edges {
|
|
|
|
switch layoutFromAttribute {
|
|
|
|
case .leftMargin:
|
|
|
|
layoutToAttribute = .left
|
|
|
|
case .rightMargin:
|
|
|
|
layoutToAttribute = .right
|
|
|
|
case .topMargin:
|
|
|
|
layoutToAttribute = .top
|
|
|
|
case .bottomMargin:
|
|
|
|
layoutToAttribute = .bottom
|
|
|
|
default:
|
|
|
|
fatalError()
|
|
|
|
}
|
2016-09-10 12:38:40 +08:00
|
|
|
} else if self.from.attributes == self.to.attributes {
|
|
|
|
layoutToAttribute = layoutFromAttribute
|
2016-09-01 21:39:08 +08:00
|
|
|
} else {
|
|
|
|
layoutToAttribute = layoutToAttributes[0]
|
2016-09-01 21:23:25 +08:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-07 19:11:36 +08:00
|
|
|
if self.to.target == nil && (layoutFromAttribute == .centerX || layoutFromAttribute == .centerY) {
|
|
|
|
layoutToAttribute = layoutFromAttribute == .centerX ? .left : .top
|
|
|
|
} else {
|
|
|
|
layoutToAttribute = layoutFromAttribute
|
|
|
|
}
|
2016-09-01 21:39:08 +08:00
|
|
|
}
|
|
|
|
#else
|
2016-09-20 12:57:16 +08:00
|
|
|
if self.from.attributes == self.to.attributes {
|
|
|
|
layoutToAttribute = layoutFromAttribute
|
|
|
|
} else if layoutToAttributes.count > 0 {
|
2016-09-01 21:23:25 +08:00
|
|
|
layoutToAttribute = layoutToAttributes[0]
|
2016-09-01 21:39:08 +08:00
|
|
|
} else {
|
|
|
|
layoutToAttribute = layoutFromAttribute
|
2016-09-01 21:23:25 +08:00
|
|
|
}
|
2016-09-01 21:39:08 +08:00
|
|
|
#endif
|
2016-08-06 18:38:02 +08:00
|
|
|
|
|
|
|
// get layout constant
|
|
|
|
let layoutConstant: CGFloat = self.constant.constraintConstantTargetValueFor(layoutAttribute: layoutToAttribute)
|
|
|
|
|
|
|
|
// get layout to
|
2016-09-01 20:27:26 +08:00
|
|
|
var layoutTo: AnyObject? = self.to.target
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-08-16 19:24:14 +08:00
|
|
|
// use superview if possible
|
|
|
|
if layoutTo == nil && layoutToAttribute != .width && layoutToAttribute != .height {
|
|
|
|
layoutTo = layoutFrom.superview
|
|
|
|
}
|
|
|
|
|
2016-08-06 18:38:02 +08:00
|
|
|
// create layout constraint
|
|
|
|
let layoutConstraint = LayoutConstraint(
|
|
|
|
item: layoutFrom,
|
|
|
|
attribute: layoutFromAttribute,
|
|
|
|
relatedBy: layoutRelation,
|
|
|
|
toItem: layoutTo,
|
|
|
|
attribute: layoutToAttribute,
|
|
|
|
multiplier: self.multiplier.constraintMultiplierTargetValue,
|
|
|
|
constant: layoutConstant
|
|
|
|
)
|
|
|
|
|
|
|
|
// set label
|
|
|
|
layoutConstraint.label = self.label
|
|
|
|
|
|
|
|
// set priority
|
|
|
|
layoutConstraint.priority = self.priority.constraintPriorityTargetValue
|
|
|
|
|
|
|
|
// set constraint
|
|
|
|
layoutConstraint.constraint = self
|
|
|
|
|
|
|
|
// append
|
2016-09-07 16:23:54 +08:00
|
|
|
self.layoutConstraints.append(layoutConstraint)
|
2016-08-06 18:38:02 +08:00
|
|
|
}
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 19:32:03 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
// MARK: Public
|
2015-07-30 14:49:47 +08:00
|
|
|
|
2016-09-14 16:59:59 +08:00
|
|
|
@available(*, deprecated:3.0, message:"Use activate().")
|
|
|
|
public func install() {
|
|
|
|
self.activate()
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, deprecated:3.0, message:"Use deactivate().")
|
|
|
|
public func uninstall() {
|
|
|
|
self.deactivate()
|
|
|
|
}
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public func activate() {
|
|
|
|
self.activateIfNeeded()
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public func deactivate() {
|
|
|
|
self.deactivateIfNeeded()
|
2015-04-12 19:32:03 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-08-04 06:46:25 +08:00
|
|
|
@discardableResult
|
|
|
|
public func update(offset: ConstraintOffsetTarget) -> Constraint {
|
|
|
|
self.constant = offset.constraintOffsetTargetValue
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
public func update(inset: ConstraintInsetTarget) -> Constraint {
|
|
|
|
self.constant = inset.constraintInsetTargetValue
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
public func update(priority: ConstraintPriorityTarget) -> Constraint {
|
|
|
|
self.priority = priority.constraintPriorityTargetValue
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:59:51 +08:00
|
|
|
@available(*, deprecated:3.0, message:"Use update(offset: ConstraintOffsetTarget) instead.")
|
2016-08-04 06:57:23 +08:00
|
|
|
public func updateOffset(amount: ConstraintOffsetTarget) -> Void { self.update(offset: amount) }
|
|
|
|
|
2016-09-14 16:59:51 +08:00
|
|
|
@available(*, deprecated:3.0, message:"Use update(inset: ConstraintInsetTarget) instead.")
|
2016-08-04 06:57:23 +08:00
|
|
|
public func updateInsets(amount: ConstraintInsetTarget) -> Void { self.update(inset: amount) }
|
|
|
|
|
2016-09-14 16:59:51 +08:00
|
|
|
@available(*, deprecated:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
|
2016-08-04 06:57:23 +08:00
|
|
|
public func updatePriority(amount: ConstraintPriorityTarget) -> Void { self.update(priority: amount) }
|
|
|
|
|
2016-09-14 16:59:51 +08:00
|
|
|
@available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
|
2016-08-04 06:57:23 +08:00
|
|
|
public func updatePriorityRequired() -> Void {}
|
|
|
|
|
2016-09-14 16:59:51 +08:00
|
|
|
@available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
|
2016-08-04 06:57:23 +08:00
|
|
|
public func updatePriorityHigh() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
|
2016-09-14 16:59:51 +08:00
|
|
|
@available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
|
2016-08-04 06:57:23 +08:00
|
|
|
public func updatePriorityMedium() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
|
2016-09-14 16:59:51 +08:00
|
|
|
@available(*, obsoleted:3.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
|
2016-08-04 06:57:23 +08:00
|
|
|
public func updatePriorityLow() -> Void { fatalError("Must be implemented by Concrete subclass.") }
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
// MARK: Internal
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-08-04 06:46:25 +08:00
|
|
|
internal func updateConstantAndPriorityIfNeeded() {
|
2016-09-07 16:23:54 +08:00
|
|
|
for layoutConstraint in self.layoutConstraints {
|
2016-08-04 06:46:25 +08:00
|
|
|
let attribute = (layoutConstraint.secondAttribute == .notAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute
|
|
|
|
layoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: attribute)
|
2016-10-07 19:11:36 +08:00
|
|
|
|
|
|
|
#if os(iOS) || os(tvOS)
|
|
|
|
let requiredPriority: UILayoutPriority = UILayoutPriorityRequired
|
|
|
|
#else
|
|
|
|
let requiredPriority: Float = 1000.0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if (layoutConstraint.priority < requiredPriority), (self.priority.constraintPriorityTargetValue != requiredPriority) {
|
|
|
|
layoutConstraint.priority = self.priority.constraintPriorityTargetValue
|
|
|
|
}
|
2016-08-04 06:46:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 18:38:02 +08:00
|
|
|
internal func activateIfNeeded(updatingExisting: Bool = false) {
|
2016-09-20 12:53:32 +08:00
|
|
|
guard let view = self.from.view else {
|
|
|
|
print("WARNING: SnapKit failed to get from view from constraint. Activate will be a no-op.")
|
|
|
|
return
|
|
|
|
}
|
2016-09-07 16:23:54 +08:00
|
|
|
let layoutConstraints = self.layoutConstraints
|
2016-09-20 12:53:32 +08:00
|
|
|
let existingLayoutConstraints = view.snp.constraints.map({ $0.layoutConstraints }).reduce([]) { $0 + $1 }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-09-10 17:51:20 +08:00
|
|
|
if updatingExisting {
|
2016-08-06 18:38:02 +08:00
|
|
|
for layoutConstraint in layoutConstraints {
|
|
|
|
let existingLayoutConstraint = existingLayoutConstraints.first { $0 == layoutConstraint }
|
|
|
|
guard let updateLayoutConstraint = existingLayoutConstraint else {
|
|
|
|
fatalError("Updated constraint could not find existing matching constraint to update: \(layoutConstraint)")
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
|
|
|
|
2016-08-06 18:38:02 +08:00
|
|
|
let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute
|
|
|
|
updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute)
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
} else {
|
|
|
|
NSLayoutConstraint.activate(layoutConstraints)
|
2016-09-20 12:53:32 +08:00
|
|
|
view.snp.add(constraints: [self])
|
2015-04-12 18:21:02 +08:00
|
|
|
}
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
internal func deactivateIfNeeded() {
|
2016-09-20 12:53:32 +08:00
|
|
|
guard let view = self.from.view else {
|
|
|
|
print("WARNING: SnapKit failed to get from view from constraint. Deactivate will be a no-op.")
|
|
|
|
return
|
|
|
|
}
|
2016-09-07 16:23:54 +08:00
|
|
|
let layoutConstraints = self.layoutConstraints
|
2016-06-15 09:49:49 +08:00
|
|
|
NSLayoutConstraint.deactivate(layoutConstraints)
|
2016-09-20 12:53:32 +08:00
|
|
|
view.snp.remove(constraints: [self])
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
|
|
|
}
|