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
|
|
|
//
|
2016-01-26 18:49:04 +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-22 20:31:40 +08:00
|
|
|
public class ConstraintMaker {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var left: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Left)
|
|
|
|
}
|
|
|
|
public var top: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Top)
|
|
|
|
}
|
|
|
|
public var bottom: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Bottom)
|
|
|
|
}
|
|
|
|
public var right: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Right)
|
|
|
|
}
|
|
|
|
public var leading: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Leading)
|
|
|
|
}
|
|
|
|
public var trailing: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Trailing)
|
|
|
|
}
|
|
|
|
public var width: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Width)
|
|
|
|
}
|
|
|
|
public var height: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Height)
|
|
|
|
}
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerX: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.CenterX)
|
|
|
|
}
|
|
|
|
public var centerY: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.CenterY)
|
|
|
|
}
|
|
|
|
public var baseline: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Baseline)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var firstBaseline: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.FirstBaseline)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var leftMargin: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.LeftMargin)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var rightMargin: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.RightMargin)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var bottomMargin: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.BottomMargin)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var leadingMargin: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.LeadingMargin)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var trailingMargin: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.TrailingMargin)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerXWithinMargins: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.CenterXWithinMargins)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerYWithinMargins: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.CenterYWithinMargins)
|
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var edges: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Edges)
|
|
|
|
}
|
|
|
|
public var size: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Size)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var margins: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.Margins)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerWithinMargins: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(ConstraintAttributes.CenterWithinMargins)
|
|
|
|
}
|
|
|
|
|
|
|
|
private let view: ConstraintView
|
|
|
|
private var descriptions = [ConstraintDescription]()
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal init(view: ConstraintView) {
|
2014-07-25 12:24:17 +08:00
|
|
|
self.view = view
|
2016-01-26 18:49:04 +08:00
|
|
|
self.view.translatesAutoresizingMaskIntoConstraints = false
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal func makeExtendableWithAttributes(attributes: ConstraintAttributes) -> ConstraintMakerExtendable {
|
|
|
|
let description = ConstraintDescription(view: self.view, attributes: attributes)
|
|
|
|
self.descriptions.append(description)
|
|
|
|
return ConstraintMakerExtendable(description)
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal static func prepareConstraints(view view: ConstraintView, @noescape closure: (make: ConstraintMaker) -> Void) -> [Constraint] {
|
|
|
|
let maker = ConstraintMaker(view: view)
|
2015-04-12 18:21:02 +08:00
|
|
|
closure(make: maker)
|
2016-01-26 18:49:04 +08:00
|
|
|
let constraints = maker.descriptions
|
|
|
|
.map { $0.constraint }
|
|
|
|
.filter { $0 != nil }
|
|
|
|
.map { $0! }
|
2015-07-30 14:49:47 +08:00
|
|
|
return constraints
|
2015-02-17 06:11:58 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal static func makeConstraints(view view: ConstraintView, @noescape closure: (make: ConstraintMaker) -> Void) {
|
|
|
|
let maker = ConstraintMaker(view: view)
|
2015-04-12 18:21:02 +08:00
|
|
|
closure(make: maker)
|
2016-01-26 18:49:04 +08:00
|
|
|
let constraints = maker.descriptions
|
|
|
|
.map { $0.constraint }
|
|
|
|
.filter { $0 != nil }
|
|
|
|
.map { $0! }
|
2015-04-12 18:21:02 +08:00
|
|
|
for constraint in constraints {
|
2016-01-26 18:49:04 +08:00
|
|
|
constraint.installIfNeeded()
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal static func remakeConstraints(view view: ConstraintView, @noescape closure: (make: ConstraintMaker) -> Void) {
|
2015-07-30 14:49:47 +08:00
|
|
|
self.removeConstraints(view: view)
|
2016-01-26 18:49:04 +08:00
|
|
|
self.makeConstraints(view: view, closure: closure)
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
2014-12-31 07:47:31 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal static func updateConstraints(view view: ConstraintView, @noescape closure: (make: ConstraintMaker) -> Void) {
|
|
|
|
let maker = ConstraintMaker(view: view)
|
2015-04-12 18:21:02 +08:00
|
|
|
closure(make: maker)
|
2016-01-26 18:49:04 +08:00
|
|
|
let constraints = maker.descriptions
|
|
|
|
.map { $0.constraint }
|
|
|
|
.filter { $0 != nil }
|
|
|
|
.map { $0! }
|
2015-04-12 18:21:02 +08:00
|
|
|
for constraint in constraints {
|
2016-01-26 18:49:04 +08:00
|
|
|
constraint.installIfNeeded(updateExisting: true)
|
2015-01-08 14:20:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
internal static func removeConstraints(view view: ConstraintView) {
|
|
|
|
|
2014-12-31 07:47:31 +08:00
|
|
|
}
|
2016-01-26 18:49:04 +08:00
|
|
|
|
2015-06-17 19:09:54 +08:00
|
|
|
}
|