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-2015 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)
|
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
|
|
|
/**
|
2015-04-12 18:21:02 +08:00
|
|
|
Used to make constraints
|
|
|
|
*/
|
2015-04-22 20:31:40 +08:00
|
|
|
public class ConstraintMaker {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// left edge
|
|
|
|
public var left: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Left) }
|
|
|
|
|
|
|
|
/// top edge
|
|
|
|
public var top: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Top) }
|
|
|
|
|
|
|
|
/// right edge
|
|
|
|
public var right: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Right) }
|
|
|
|
|
|
|
|
/// bottom edge
|
|
|
|
public var bottom: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Bottom) }
|
|
|
|
|
|
|
|
/// leading edge
|
|
|
|
public var leading: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Leading) }
|
|
|
|
|
|
|
|
/// trailing edge
|
|
|
|
public var trailing: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Trailing) }
|
|
|
|
|
|
|
|
/// width dimension
|
|
|
|
public var width: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Width) }
|
|
|
|
|
|
|
|
/// height dimension
|
|
|
|
public var height: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Height) }
|
|
|
|
|
|
|
|
/// centerX dimension
|
|
|
|
public var centerX: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterX) }
|
|
|
|
|
|
|
|
/// centerY dimension
|
|
|
|
public var centerY: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterY) }
|
|
|
|
|
|
|
|
/// baseline position
|
|
|
|
public var baseline: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Baseline) }
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/// firse baseline position
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var firstBaseline: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.FirstBaseline) }
|
|
|
|
|
|
|
|
/// left margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var leftMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.LeftMargin) }
|
|
|
|
|
|
|
|
/// right margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var rightMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.RightMargin) }
|
|
|
|
|
|
|
|
/// top margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var topMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.TopMargin) }
|
|
|
|
|
|
|
|
/// bottom margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var bottomMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.BottomMargin) }
|
|
|
|
|
|
|
|
/// leading margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var leadingMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.LeadingMargin) }
|
|
|
|
|
|
|
|
/// trailing margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var trailingMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.TrailingMargin) }
|
|
|
|
|
|
|
|
/// centerX within margins
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var centerXWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterXWithinMargins) }
|
|
|
|
|
|
|
|
/// centerY within margins
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-12 18:21:02 +08:00
|
|
|
public var centerYWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterYWithinMargins) }
|
2015-05-21 18:05:38 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/// top + left + bottom + right edges
|
|
|
|
public var edges: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Edges) }
|
|
|
|
|
|
|
|
/// width + height dimensions
|
|
|
|
public var size: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Size) }
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
// centerX + centerY positions
|
|
|
|
public var center: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Center) }
|
|
|
|
|
|
|
|
// top + left + bottom + right margins
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-05-21 18:05:38 +08:00
|
|
|
public var margins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Margins) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
// centerX + centerY within margins
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-05-21 18:05:38 +08:00
|
|
|
public var centerWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterWithinMargins) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-10-12 14:21:40 +08:00
|
|
|
internal init(view: View, location: SourceLocation) {
|
2014-07-25 12:24:17 +08:00
|
|
|
self.view = view
|
2015-10-12 14:21:40 +08:00
|
|
|
self.location = location
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
|
2015-10-12 14:21:40 +08:00
|
|
|
internal let location: SourceLocation
|
2015-04-12 19:32:03 +08:00
|
|
|
internal let view: View
|
2015-04-12 18:21:02 +08:00
|
|
|
internal var constraintDescriptions = [ConstraintDescription]()
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-10-12 13:53:46 +08:00
|
|
|
internal func makeConstraintDescription(attributes: ConstraintAttributes) -> ConstraintDescriptionExtendable {
|
2015-01-08 09:58:12 +08:00
|
|
|
let item = ConstraintItem(object: self.view, attributes: attributes)
|
2015-04-12 18:21:02 +08:00
|
|
|
let constraintDescription = ConstraintDescription(fromItem: item)
|
|
|
|
self.constraintDescriptions.append(constraintDescription)
|
2015-10-12 13:53:46 +08:00
|
|
|
return ConstraintDescriptionExtendable(constraintDescription)
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
|
2015-10-12 14:21:40 +08:00
|
|
|
internal class func prepareConstraints(view view: View, location: SourceLocation, @noescape closure: (make: ConstraintMaker) -> Void) -> [Constraint] {
|
2015-11-08 16:18:01 +08:00
|
|
|
let maker = ConstraintMaker(view: view, location: location)
|
2015-04-12 18:21:02 +08:00
|
|
|
closure(make: maker)
|
|
|
|
|
2015-07-30 14:49:47 +08:00
|
|
|
let constraints = maker.constraintDescriptions.map { $0.constraint }
|
|
|
|
for constraint in constraints {
|
2015-10-12 14:21:40 +08:00
|
|
|
constraint.makerLocation = maker.location
|
2015-07-30 14:49:47 +08:00
|
|
|
}
|
|
|
|
return constraints
|
2015-02-17 06:11:58 +08:00
|
|
|
}
|
|
|
|
|
2015-10-12 14:21:40 +08:00
|
|
|
internal class func makeConstraints(view view: View, location: SourceLocation, @noescape closure: (make: ConstraintMaker) -> Void) {
|
2014-07-30 08:55:31 +08:00
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
2015-10-12 14:21:40 +08:00
|
|
|
let maker = ConstraintMaker(view: view, location: location)
|
2015-04-12 18:21:02 +08:00
|
|
|
closure(make: maker)
|
|
|
|
|
2015-04-12 19:32:03 +08:00
|
|
|
let constraints = maker.constraintDescriptions.map { $0.constraint as! ConcreteConstraint }
|
2015-04-12 18:21:02 +08:00
|
|
|
for constraint in constraints {
|
2015-10-12 14:21:40 +08:00
|
|
|
constraint.makerLocation = maker.location
|
2015-02-17 06:11:58 +08:00
|
|
|
constraint.installOnView(updateExisting: false)
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 16:18:01 +08:00
|
|
|
internal class func remakeConstraints(view view: View, location: SourceLocation, @noescape closure: (make: ConstraintMaker) -> Void) {
|
2014-07-30 08:55:31 +08:00
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
2015-10-12 14:21:40 +08:00
|
|
|
let maker = ConstraintMaker(view: view, location: location)
|
2015-04-12 18:21:02 +08:00
|
|
|
closure(make: maker)
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-07-30 14:49:47 +08:00
|
|
|
self.removeConstraints(view: view)
|
2015-04-12 19:32:03 +08:00
|
|
|
let constraints = maker.constraintDescriptions.map { $0.constraint as! ConcreteConstraint }
|
2015-04-12 18:21:02 +08:00
|
|
|
for constraint in constraints {
|
2015-10-12 14:21:40 +08:00
|
|
|
constraint.makerLocation = maker.location
|
2015-02-17 06:11:58 +08:00
|
|
|
constraint.installOnView(updateExisting: false)
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
}
|
2014-12-31 07:47:31 +08:00
|
|
|
|
2015-10-12 14:21:40 +08:00
|
|
|
internal class func updateConstraints(view view: View, location: SourceLocation, @noescape closure: (make: ConstraintMaker) -> Void) {
|
2015-01-08 14:20:34 +08:00
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
2015-10-12 14:21:40 +08:00
|
|
|
let maker = ConstraintMaker(view: view, location: location)
|
2015-04-12 18:21:02 +08:00
|
|
|
closure(make: maker)
|
2015-01-08 14:20:34 +08:00
|
|
|
|
2015-04-12 19:32:03 +08:00
|
|
|
let constraints = maker.constraintDescriptions.map { $0.constraint as! ConcreteConstraint}
|
2015-04-12 18:21:02 +08:00
|
|
|
for constraint in constraints {
|
2015-10-12 14:21:40 +08:00
|
|
|
constraint.makerLocation = maker.location
|
2015-02-17 06:11:58 +08:00
|
|
|
constraint.installOnView(updateExisting: true)
|
2015-01-08 14:20:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:35:48 +08:00
|
|
|
internal class func removeConstraints(view view: View) {
|
2015-04-11 13:45:59 +08:00
|
|
|
for existingLayoutConstraint in view.snp_installedLayoutConstraints {
|
2015-04-12 19:32:03 +08:00
|
|
|
existingLayoutConstraint.snp_constraint?.uninstall()
|
2014-12-31 07:47:31 +08:00
|
|
|
}
|
|
|
|
}
|
2015-06-17 19:09:54 +08:00
|
|
|
}
|