2014-09-23 12:28:00 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// SnapKit
|
2014-09-23 12:28:00 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// Copyright (c) 2011-2015 SnapKit Team - https://github.com/SnapKit
|
2014-09-23 12:28:00 +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.
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
import UIKit
|
|
|
|
public typealias View = UIView
|
|
|
|
#else
|
|
|
|
import AppKit
|
|
|
|
public typealias View = NSView
|
|
|
|
#endif
|
|
|
|
|
2015-04-11 16:33:13 +08:00
|
|
|
/**
|
2015-04-12 18:21:02 +08:00
|
|
|
Used to expose public API on views
|
|
|
|
*/
|
2014-09-23 12:28:00 +08:00
|
|
|
public extension View {
|
2015-01-08 14:04:29 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/// left edge
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_left: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Left) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// top edge
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_top: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Top) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// right edge
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_right: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Right) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// bottom edge
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_bottom: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Bottom) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// leading edge
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_leading: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Leading) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// trailing edge
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_trailing: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Trailing) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// width dimension
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_width: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Width) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// height dimension
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_height: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Height) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// centerX position
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_centerX: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterX) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// centerY position
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_centerY: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterY) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// baseline position
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_baseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Baseline) }
|
2014-09-23 12:28:00 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/// first baseline position
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_firstBaseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.FirstBaseline) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// left margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_leftMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeftMargin) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// right margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_rightMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.RightMargin) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// top margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_topMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TopMargin) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// bottom margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_bottomMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.BottomMargin) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// leading margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_leadingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeadingMargin) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// trailing margin
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_trailingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TrailingMargin) }
|
2015-01-08 14:04:29 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/// centerX within margins
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_centerXWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterXWithinMargins) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/// centerY within margins
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_centerYWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterYWithinMargins) }
|
2014-10-29 05:30:07 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
// top + left + bottom + right edges
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_edges: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Edges) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
// width + height dimensions
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_size: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Size) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
// centerX + centerY positions
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_center: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Center) }
|
2014-09-23 12:28:00 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
// top + left + bottom + right margins
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2015-04-22 20:31:40 +08:00
|
|
|
public var snp_margins: ConstraintItem { return ConstraintItem(object: self, attributes: 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-04-22 20:31:40 +08:00
|
|
|
public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) }
|
2015-04-12 18:21:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Prepares constraints with a `ConstraintMaker` and returns the made constraints but does not install them.
|
|
|
|
|
|
|
|
:param: closure that will be passed the `ConstraintMaker` to make the constraints with
|
|
|
|
|
|
|
|
:returns: the constraints made
|
|
|
|
*/
|
2015-07-30 14:49:47 +08:00
|
|
|
public func snp_prepareConstraints(file: String = __FILE__, line: UInt = __LINE__, @noescape closure: (make: ConstraintMaker) -> Void) -> [Constraint] {
|
|
|
|
return ConstraintMaker.prepareConstraints(view: self, file: file, line: line, closure: closure)
|
2015-02-17 06:11:58 +08:00
|
|
|
}
|
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/**
|
|
|
|
Makes constraints with a `ConstraintMaker` and installs them along side any previous made constraints.
|
|
|
|
|
|
|
|
:param: closure that will be passed the `ConstraintMaker` to make the constraints with
|
|
|
|
*/
|
2015-07-30 14:49:47 +08:00
|
|
|
public func snp_makeConstraints(file: String = __FILE__, line: UInt = __LINE__, @noescape closure: (make: ConstraintMaker) -> Void) -> Void {
|
|
|
|
ConstraintMaker.makeConstraints(view: self, file: file, line: line, closure: closure)
|
2014-09-23 12:28:00 +08:00
|
|
|
}
|
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/**
|
|
|
|
Updates constraints with a `ConstraintMaker` that will replace existing constraints that match and install new ones.
|
|
|
|
|
|
|
|
For constraints to match only the constant can be updated.
|
|
|
|
|
|
|
|
:param: closure that will be passed the `ConstraintMaker` to update the constraints with
|
|
|
|
*/
|
2015-07-30 14:49:47 +08:00
|
|
|
public func snp_updateConstraints(file: String = __FILE__, line: UInt = __LINE__, @noescape closure: (make: ConstraintMaker) -> Void) -> Void {
|
|
|
|
ConstraintMaker.updateConstraints(view: self, file: file, line: line, closure: closure)
|
2015-01-08 14:04:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/**
|
|
|
|
Remakes constraints with a `ConstraintMaker` that will first remove all previously made constraints and make and install new ones.
|
|
|
|
|
|
|
|
:param: closure that will be passed the `ConstraintMaker` to remake the constraints with
|
|
|
|
*/
|
2015-07-30 14:49:47 +08:00
|
|
|
public func snp_remakeConstraints(file: String = __FILE__, line: UInt = __LINE__, @noescape closure: (make: ConstraintMaker) -> Void) -> Void {
|
|
|
|
ConstraintMaker.remakeConstraints(view: self, file: file, line: line, closure: closure)
|
2014-09-23 12:28:00 +08:00
|
|
|
}
|
2014-10-06 08:32:41 +08:00
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
/**
|
|
|
|
Removes all previously made constraints.
|
|
|
|
*/
|
2015-04-22 20:31:40 +08:00
|
|
|
public func snp_removeConstraints() {
|
2015-07-30 14:49:47 +08:00
|
|
|
ConstraintMaker.removeConstraints(view: self)
|
2014-10-06 08:32:41 +08:00
|
|
|
}
|
2015-01-08 14:04:29 +08:00
|
|
|
|
2015-04-22 20:31:40 +08:00
|
|
|
internal var snp_installedLayoutConstraints: [LayoutConstraint] {
|
2015-01-08 14:04:29 +08:00
|
|
|
get {
|
2015-04-11 13:13:02 +08:00
|
|
|
if let constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? [LayoutConstraint] {
|
2015-02-14 18:37:27 +08:00
|
|
|
return constraints
|
2015-01-08 14:04:29 +08:00
|
|
|
}
|
2015-04-11 13:13:02 +08:00
|
|
|
return []
|
2015-01-08 14:04:29 +08:00
|
|
|
}
|
|
|
|
set {
|
2015-06-17 19:09:54 +08:00
|
|
|
objc_setAssociatedObject(self, &installedLayoutConstraintsKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
2015-01-08 14:04:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:14:52 +08:00
|
|
|
private var installedLayoutConstraintsKey = ""
|