Fixed iOS 9 layout guides, and better support for legacy platforms

This commit is contained in:
Robert Payne 2015-09-22 19:38:50 +12:00
parent de954c0c12
commit ba26480317
7 changed files with 43 additions and 58 deletions

View File

@ -138,10 +138,16 @@ internal class ConcreteConstraint: Constraint {
} }
internal override func activate() -> Void { internal override func activate() -> Void {
guard #available(iOS 8.0, OSX 10.10, *), self.installInfo != nil else { guard self.installInfo != nil else {
self.install() self.install()
return return
} }
#if SNAPKIT_DEPLOYMENT_LEGACY
guard #available(iOS 8.0, OSX 10.10, *) else {
self.install()
return
}
#endif
let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint] let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint]
if layoutConstraints.count > 0 { if layoutConstraints.count > 0 {
NSLayoutConstraint.activateConstraints(layoutConstraints) NSLayoutConstraint.activateConstraints(layoutConstraints)
@ -149,10 +155,14 @@ internal class ConcreteConstraint: Constraint {
} }
internal override func deactivate() -> Void { internal override func deactivate() -> Void {
guard #available(iOS 8.0, OSX 10.10, *), self.installInfo != nil else { guard self.installInfo != nil else {
self.install()
return return
} }
#if SNAPKIT_DEPLOYMENT_LEGACY
guard #available(iOS 8.0, OSX 10.10, *) else {
return
}
#endif
let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint] let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint]
if layoutConstraints.count > 0 { if layoutConstraints.count > 0 {
NSLayoutConstraint.deactivateConstraints(layoutConstraints) NSLayoutConstraint.deactivateConstraints(layoutConstraints)
@ -241,7 +251,11 @@ internal class ConcreteConstraint: Constraint {
let layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant) let layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
// get layout to // get layout to
var layoutTo: View? = self.toItem.view #if os(iOS)
var layoutTo: AnyObject? = self.toItem.view ?? self.toItem.layoutSupport
#else
var layoutTo: AnyObject? = self.toItem.view
#endif
if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height { if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
layoutTo = installOnView layoutTo = installOnView
} }

View File

@ -66,7 +66,6 @@ internal struct ConstraintAttributes: OptionSetType, BooleanType {
internal static var CenterY: ConstraintAttributes { return self.init(512) } internal static var CenterY: ConstraintAttributes { return self.init(512) }
internal static var Baseline: ConstraintAttributes { return self.init(1024) } internal static var Baseline: ConstraintAttributes { return self.init(1024) }
#if os(iOS)
@available(iOS 8.0, *) @available(iOS 8.0, *)
internal static var FirstBaseline: ConstraintAttributes { return self.init(2048) } internal static var FirstBaseline: ConstraintAttributes { return self.init(2048) }
@available(iOS 8.0, *) @available(iOS 8.0, *)
@ -85,7 +84,6 @@ internal struct ConstraintAttributes: OptionSetType, BooleanType {
internal static var CenterXWithinMargins: ConstraintAttributes { return self.init(262144) } internal static var CenterXWithinMargins: ConstraintAttributes { return self.init(262144) }
@available(iOS 8.0, *) @available(iOS 8.0, *)
internal static var CenterYWithinMargins: ConstraintAttributes { return self.init(524288) } internal static var CenterYWithinMargins: ConstraintAttributes { return self.init(524288) }
#endif
// aggregates // aggregates
@ -93,12 +91,11 @@ internal struct ConstraintAttributes: OptionSetType, BooleanType {
internal static var Size: ConstraintAttributes { return self.init(192) } internal static var Size: ConstraintAttributes { return self.init(192) }
internal static var Center: ConstraintAttributes { return self.init(768) } internal static var Center: ConstraintAttributes { return self.init(768) }
#if os(iOS)
@available(iOS 8.0, *) @available(iOS 8.0, *)
internal static var Margins: ConstraintAttributes { return self.init(61440) } internal static var Margins: ConstraintAttributes { return self.init(61440) }
@available(iOS 8.0, *) @available(iOS 8.0, *)
internal static var CenterWithinMargins: ConstraintAttributes { return self.init(786432) } internal static var CenterWithinMargins: ConstraintAttributes { return self.init(786432) }
#endif
internal var layoutAttributes:[NSLayoutAttribute] { internal var layoutAttributes:[NSLayoutAttribute] {
var attrs = [NSLayoutAttribute]() var attrs = [NSLayoutAttribute]()
@ -135,10 +132,13 @@ internal struct ConstraintAttributes: OptionSetType, BooleanType {
if (self.contains(ConstraintAttributes.Baseline)) { if (self.contains(ConstraintAttributes.Baseline)) {
attrs.append(.Baseline) attrs.append(.Baseline)
} }
#if os(iOS) #if os(iOS)
#if SNAPKIT_DEPLOYMENT_LEGACY
guard #available(iOS 8.0, *) else { guard #available(iOS 8.0, *) else {
fatalError("Constrant attribute unavaialble for running platform and deployment target.") return attrs
} }
#endif
if (self.contains(ConstraintAttributes.FirstBaseline)) { if (self.contains(ConstraintAttributes.FirstBaseline)) {
attrs.append(.FirstBaseline) attrs.append(.FirstBaseline)
} }
@ -167,6 +167,7 @@ internal struct ConstraintAttributes: OptionSetType, BooleanType {
attrs.append(.CenterYWithinMargins) attrs.append(.CenterYWithinMargins)
} }
#endif #endif
return attrs return attrs
} }
} }

View File

@ -93,9 +93,8 @@ public protocol ConstraintDescriptionRelatable: class {
func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable
func equalTo(other: View) -> ConstraintDescriptionEditable func equalTo(other: View) -> ConstraintDescriptionEditable
#if os(iOS) @available(iOS 7.0, *)
func equalTo(other: UILayoutSupport) -> ConstraintDescriptionEditable func equalTo(other: LayoutSupport) -> ConstraintDescriptionEditable
#endif
func equalTo(other: Float) -> ConstraintDescriptionEditable func equalTo(other: Float) -> ConstraintDescriptionEditable
func equalTo(other: Double) -> ConstraintDescriptionEditable func equalTo(other: Double) -> ConstraintDescriptionEditable
func equalTo(other: CGFloat) -> ConstraintDescriptionEditable func equalTo(other: CGFloat) -> ConstraintDescriptionEditable
@ -107,9 +106,8 @@ public protocol ConstraintDescriptionRelatable: class {
func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable
#if os(iOS) @available(iOS 7.0, *)
func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionEditable func lessThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable
#endif
func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable
func lessThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable func lessThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable
func lessThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable func lessThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable
@ -121,9 +119,8 @@ public protocol ConstraintDescriptionRelatable: class {
func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable
#if os(iOS) @available(iOS 7.0, *)
func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionEditable func greaterThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable
#endif
func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable
func greaterThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable func greaterThanOrEqualTo(other: Double) -> ConstraintDescriptionEditable
func greaterThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable func greaterThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionEditable
@ -152,7 +149,6 @@ public protocol ConstraintDescriptionExtendable: ConstraintDescriptionRelatable
var centerY: ConstraintDescriptionExtendable { get } var centerY: ConstraintDescriptionExtendable { get }
var baseline: ConstraintDescriptionExtendable { get } var baseline: ConstraintDescriptionExtendable { get }
#if os(iOS)
@available(iOS 8.0, *) @available(iOS 8.0, *)
var firstBaseline: ConstraintDescriptionExtendable { get } var firstBaseline: ConstraintDescriptionExtendable { get }
@available(iOS 8.0, *) @available(iOS 8.0, *)
@ -171,7 +167,6 @@ public protocol ConstraintDescriptionExtendable: ConstraintDescriptionRelatable
var centerXWithinMargins: ConstraintDescriptionExtendable { get } var centerXWithinMargins: ConstraintDescriptionExtendable { get }
@available(iOS 8.0, *) @available(iOS 8.0, *)
var centerYWithinMargins: ConstraintDescriptionExtendable { get } var centerYWithinMargins: ConstraintDescriptionExtendable { get }
#endif
} }
/** /**
@ -191,7 +186,6 @@ internal class ConstraintDescription: ConstraintDescriptionExtendable, Constrain
internal var centerY: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterY) } internal var centerY: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterY) }
internal var baseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Baseline) } internal var baseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Baseline) }
#if os(iOS)
@available(iOS 8.0, *) @available(iOS 8.0, *)
internal var firstBaseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) } internal var firstBaseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
@available(iOS 8.0, *) @available(iOS 8.0, *)
@ -210,7 +204,6 @@ internal class ConstraintDescription: ConstraintDescriptionExtendable, Constrain
internal var centerXWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) } internal var centerXWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
@available(iOS 8.0, *) @available(iOS 8.0, *)
internal var centerYWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) } internal var centerYWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
#endif
// MARK: initializer // MARK: initializer
@ -227,11 +220,10 @@ internal class ConstraintDescription: ConstraintDescriptionExtendable, Constrain
internal func equalTo(other: View) -> ConstraintDescriptionEditable { internal func equalTo(other: View) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
#if os(iOS) @available(iOS 7.0, *)
internal func equalTo(other: UILayoutSupport) -> ConstraintDescriptionEditable { internal func equalTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
#endif
internal func equalTo(other: Float) -> ConstraintDescriptionEditable { internal func equalTo(other: Float) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
@ -265,11 +257,10 @@ internal class ConstraintDescription: ConstraintDescriptionExtendable, Constrain
internal func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable { internal func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
#if os(iOS) @available(iOS 7.0, *)
internal func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionEditable { internal func lessThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
#endif
internal func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable { internal func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
@ -303,11 +294,10 @@ internal class ConstraintDescription: ConstraintDescriptionExtendable, Constrain
internal func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable { internal func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
#if os(iOS) @available(iOS 7.0, *)
internal func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionEditable { internal func greaterThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
#endif
internal func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable { internal func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionEditable {
return self.constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
@ -552,14 +542,11 @@ internal class ConstraintDescription: ConstraintDescriptionExtendable, Constrain
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation) return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
} }
#if os(iOS) @available(iOS 7.0, *)
private func constrainTo(other: LayoutSupport, relation: ConstraintRelation) -> ConstraintDescription {
private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> ConstraintDescription {
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation) return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
} }
#endif
private func constrainTo(other: Float, relation: ConstraintRelation) -> ConstraintDescription { private func constrainTo(other: Float, relation: ConstraintRelation) -> ConstraintDescription {
self.constant = other self.constant = other
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)

View File

@ -44,13 +44,10 @@ public class ConstraintItem {
return self.object as? View return self.object as? View
} }
#if os(iOS) @available(iOS 7.0, *)
internal var layoutSupport: LayoutSupport? {
internal var layoutSupport: UILayoutSupport? { return self.object as? LayoutSupport
return self.object as? UILayoutSupport
} }
#endif
} }

View File

@ -65,8 +65,6 @@ public class ConstraintMaker {
/// baseline position /// baseline position
public var baseline: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Baseline) } public var baseline: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Baseline) }
#if os(iOS)
/// firse baseline position /// firse baseline position
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var firstBaseline: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.FirstBaseline) } public var firstBaseline: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.FirstBaseline) }
@ -103,8 +101,6 @@ public class ConstraintMaker {
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var centerYWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterYWithinMargins) } public var centerYWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterYWithinMargins) }
#endif
/// top + left + bottom + right edges /// top + left + bottom + right edges
public var edges: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Edges) } public var edges: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Edges) }
@ -114,8 +110,6 @@ public class ConstraintMaker {
// centerX + centerY positions // centerX + centerY positions
public var center: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Center) } public var center: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Center) }
#if os(iOS)
// top + left + bottom + right margins // top + left + bottom + right margins
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var margins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Margins) } public var margins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Margins) }
@ -124,8 +118,6 @@ public class ConstraintMaker {
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var centerWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterWithinMargins) } public var centerWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterWithinMargins) }
#endif
internal init(view: View, file: String, line: UInt) { internal init(view: View, file: String, line: UInt) {
self.view = view self.view = view
self.file = file self.file = file

View File

@ -24,9 +24,11 @@
#if os(iOS) #if os(iOS)
import UIKit import UIKit
public typealias InterfaceLayoutDirection = UIUserInterfaceLayoutDirection public typealias InterfaceLayoutDirection = UIUserInterfaceLayoutDirection
public typealias LayoutSupport = UILayoutSupport
#else #else
import AppKit import AppKit
public typealias InterfaceLayoutDirection = NSUserInterfaceLayoutDirection public typealias InterfaceLayoutDirection = NSUserInterfaceLayoutDirection
public class LayoutSupport {}
#endif #endif
/** /**

View File

@ -67,8 +67,6 @@ public extension View {
/// baseline position /// baseline position
public var snp_baseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Baseline) } public var snp_baseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Baseline) }
#if os(iOS)
/// first baseline position /// first baseline position
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var snp_firstBaseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.FirstBaseline) } public var snp_firstBaseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.FirstBaseline) }
@ -105,8 +103,6 @@ public extension View {
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var snp_centerYWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterYWithinMargins) } public var snp_centerYWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterYWithinMargins) }
#endif
// top + left + bottom + right edges // top + left + bottom + right edges
public var snp_edges: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Edges) } public var snp_edges: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Edges) }
@ -116,8 +112,6 @@ public extension View {
// centerX + centerY positions // centerX + centerY positions
public var snp_center: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Center) } public var snp_center: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Center) }
#if os(iOS)
// top + left + bottom + right margins // top + left + bottom + right margins
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var snp_margins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Margins) } public var snp_margins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Margins) }
@ -126,8 +120,6 @@ public extension View {
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) } public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) }
#endif
/** /**
Prepares constraints with a `ConstraintMaker` and returns the made constraints but does not install them. Prepares constraints with a `ConstraintMaker` and returns the made constraints but does not install them.