Merge branch 'develop' of github.com:SnapKit/SnapKit into develop

This commit is contained in:
Robert Payne 2019-08-05 18:16:43 +12:00
commit fbd58b106a
15 changed files with 188 additions and 1 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ Examples/
Gemfile Gemfile
Gemfile.lock Gemfile.lock
*.sketch *.sketch
.swiftpm

View File

@ -27,11 +27,19 @@ import PackageDescription
let package = Package( let package = Package(
name: "SnapKit", name: "SnapKit",
platforms: [
.iOS(.v10),
.macOS(.v10_12),
.tvOS(.v10)
],
products: [ products: [
.library(name: "SnapKit", targets: ["SnapKit"]), .library(name: "SnapKit", targets: ["SnapKit"]),
], ],
targets: [ targets: [
.target(name: "SnapKit", path: "Source"), .target(name: "SnapKit", path: "Source"),
.testTarget(name: "SnapKitTests", dependencies: ["SnapKit"]), .testTarget(name: "SnapKitTests", dependencies: ["SnapKit"]),
],
swiftLanguageVersions: [
.v5
] ]
) )

View File

@ -114,6 +114,13 @@ class MyViewController: UIViewController {
} }
``` ```
### Playground
You can try SnapKit in Playground.
**Note:**
> To try SnapKit in playground, open `SnapKit.xcworkspace` and build SnapKit.framework for any simulator first.
### Resources ### Resources
- [Documentation](http://snapkit.io/docs/) - [Documentation](http://snapkit.io/docs/)

View File

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Workspace <Workspace
version = "1.0"> version = "1.0">
<FileRef
location = "group:SnapKitPlayground.playground">
</FileRef>
<FileRef <FileRef
location = "container:SnapKit.xcodeproj"> location = "container:SnapKit.xcodeproj">
</FileRef> </FileRef>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

View File

@ -0,0 +1,29 @@
//: A UIKit based Playground for presenting user interface
// To use this playground, build SnapKit.framework for any simulator first.
import SnapKit
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
label.snp.makeConstraints { (make) in
make.left.equalToSuperview().offset(150)
make.top.equalToSuperview().offset(200)
make.size.equalTo(CGSize(width: 200, height: 20))
}
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' executeOnSourceChanges='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>

View File

@ -129,6 +129,32 @@ public final class Constraint {
default: default:
fatalError() fatalError()
} }
} else if self.from.attributes == .directionalEdges && self.to.attributes == .directionalMargins {
switch layoutFromAttribute {
case .leading:
layoutToAttribute = .leadingMargin
case .trailing:
layoutToAttribute = .trailingMargin
case .top:
layoutToAttribute = .topMargin
case .bottom:
layoutToAttribute = .bottomMargin
default:
fatalError()
}
} else if self.from.attributes == .directionalMargins && self.to.attributes == .directionalEdges {
switch layoutFromAttribute {
case .leadingMargin:
layoutToAttribute = .leading
case .trailingMargin:
layoutToAttribute = .trailing
case .topMargin:
layoutToAttribute = .top
case .bottomMargin:
layoutToAttribute = .bottom
default:
fatalError()
}
} else if self.from.attributes == self.to.attributes { } else if self.from.attributes == self.to.attributes {
layoutToAttribute = layoutFromAttribute layoutToAttribute = layoutFromAttribute
} else { } else {

View File

@ -99,12 +99,16 @@ internal struct ConstraintAttributes : OptionSet, ExpressibleByIntegerLiteral {
// aggregates // aggregates
internal static var edges: ConstraintAttributes { return 15 } internal static var edges: ConstraintAttributes { return 15 }
internal static var directionalEdges: ConstraintAttributes { return 58 }
internal static var size: ConstraintAttributes { return 192 } internal static var size: ConstraintAttributes { return 192 }
internal static var center: ConstraintAttributes { return 768 } internal static var center: ConstraintAttributes { return 768 }
@available(iOS 8.0, *) @available(iOS 8.0, *)
internal static var margins: ConstraintAttributes { return 61440 } internal static var margins: ConstraintAttributes { return 61440 }
@available(iOS 8.0, *)
internal static var directionalMargins: ConstraintAttributes { return 245760 }
@available(iOS 8.0, *) @available(iOS 8.0, *)
internal static var centerWithinMargins: ConstraintAttributes { return 786432 } internal static var centerWithinMargins: ConstraintAttributes { return 786432 }

View File

@ -99,6 +99,10 @@ extension ConstraintBasicAttributesDSL {
return ConstraintItem(target: self.target, attributes: ConstraintAttributes.edges) return ConstraintItem(target: self.target, attributes: ConstraintAttributes.edges)
} }
public var directionalEdges: ConstraintItem {
return ConstraintItem(target: self.target, attributes: ConstraintAttributes.directionalEdges)
}
public var size: ConstraintItem { public var size: ConstraintItem {
return ConstraintItem(target: self.target, attributes: ConstraintAttributes.size) return ConstraintItem(target: self.target, attributes: ConstraintAttributes.size)
} }
@ -177,6 +181,11 @@ extension ConstraintAttributesDSL {
return ConstraintItem(target: self.target, attributes: ConstraintAttributes.margins) return ConstraintItem(target: self.target, attributes: ConstraintAttributes.margins)
} }
@available(iOS 8.0, *)
public var directionalMargins: ConstraintItem {
return ConstraintItem(target: self.target, attributes: ConstraintAttributes.directionalMargins)
}
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var centerWithinMargins: ConstraintItem { public var centerWithinMargins: ConstraintItem {
return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerWithinMargins) return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerWithinMargins)

View File

@ -126,6 +126,9 @@ public class ConstraintMaker {
public var edges: ConstraintMakerExtendable { public var edges: ConstraintMakerExtendable {
return self.makeExtendableWithAttributes(.edges) return self.makeExtendableWithAttributes(.edges)
} }
public var directionalEdges: ConstraintMakerExtendable {
return self.makeExtendableWithAttributes(.directionalEdges)
}
public var size: ConstraintMakerExtendable { public var size: ConstraintMakerExtendable {
return self.makeExtendableWithAttributes(.size) return self.makeExtendableWithAttributes(.size)
} }
@ -138,6 +141,11 @@ public class ConstraintMaker {
return self.makeExtendableWithAttributes(.margins) return self.makeExtendableWithAttributes(.margins)
} }
@available(iOS 8.0, *)
public var directionalMargins: ConstraintMakerExtendable {
return self.makeExtendableWithAttributes(.directionalMargins)
}
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var centerWithinMargins: ConstraintMakerExtendable { public var centerWithinMargins: ConstraintMakerExtendable {
return self.makeExtendableWithAttributes(.centerWithinMargins) return self.makeExtendableWithAttributes(.centerWithinMargins)

View File

@ -149,6 +149,10 @@ public class ConstraintMakerExtendable: ConstraintMakerRelatable {
self.description.attributes += .edges self.description.attributes += .edges
return self return self
} }
public var directionalEdges: ConstraintMakerExtendable {
self.description.attributes += .directionalEdges
return self
}
public var size: ConstraintMakerExtendable { public var size: ConstraintMakerExtendable {
self.description.attributes += .size self.description.attributes += .size
return self return self
@ -160,6 +164,12 @@ public class ConstraintMakerExtendable: ConstraintMakerRelatable {
return self return self
} }
@available(iOS 8.0, *)
public var directionalMargins: ConstraintMakerExtendable {
self.description.attributes += .directionalMargins
return self
}
@available(iOS 8.0, *) @available(iOS 8.0, *)
public var centerWithinMargins: ConstraintMakerExtendable { public var centerWithinMargins: ConstraintMakerExtendable {
self.description.attributes += .centerWithinMargins self.description.attributes += .centerWithinMargins

View File

@ -45,7 +45,9 @@ public class ConstraintMakerRelatable {
other.attributes.layoutAttributes.count <= 1 || other.attributes.layoutAttributes.count <= 1 ||
other.attributes.layoutAttributes == self.description.attributes.layoutAttributes || other.attributes.layoutAttributes == self.description.attributes.layoutAttributes ||
other.attributes == .edges && self.description.attributes == .margins || other.attributes == .edges && self.description.attributes == .margins ||
other.attributes == .margins && self.description.attributes == .edges else { other.attributes == .margins && self.description.attributes == .edges ||
other.attributes == .directionalEdges && self.description.attributes == .directionalMargins ||
other.attributes == .directionalMargins && self.description.attributes == .directionalEdges else {
fatalError("Cannot constraint to multiple non identical attributes. (\(file), \(line))"); fatalError("Cannot constraint to multiple non identical attributes. (\(file), \(line))");
} }

View File

@ -575,6 +575,28 @@ class SnapKitTests: XCTestCase {
XCTAssert(fromAttributes == [.top, .left, .bottom, .right]) XCTAssert(fromAttributes == [.top, .left, .bottom, .right])
XCTAssert(toAttributes == [.top, .left, .bottom, .right]) XCTAssert(toAttributes == [.top, .left, .bottom, .right])
} }
func testDirectionalEdgesToDirectionalEdges() {
var fromAttributes = Set<LayoutAttribute>()
var toAttributes = Set<LayoutAttribute>()
let view = View()
self.container.addSubview(view)
view.snp.remakeConstraints { (make) -> Void in
make.directionalEdges.equalTo(self.container.snp.directionalEdges)
}
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
fromAttributes.insert(constraint.firstAttribute)
toAttributes.insert(constraint.secondAttribute)
}
XCTAssert(fromAttributes == [.top, .leading, .bottom, .trailing])
XCTAssert(toAttributes == [.top, .leading, .bottom, .trailing])
}
#if os(iOS) || os(tvOS) #if os(iOS) || os(tvOS)
func testEdgesToMargins() { func testEdgesToMargins() {
@ -616,6 +638,46 @@ class SnapKitTests: XCTestCase {
XCTAssert(fromAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin]) XCTAssert(fromAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin])
} }
func testDirectionalEdgesToDirectionalMargins() {
var fromAttributes = Set<LayoutAttribute>()
var toAttributes = Set<LayoutAttribute>()
let view = View()
self.container.addSubview(view)
view.snp.remakeConstraints { (make) -> Void in
make.directionalEdges.equalTo(self.container.snp.directionalMargins)
}
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
fromAttributes.insert(constraint.firstAttribute)
toAttributes.insert(constraint.secondAttribute)
}
XCTAssert(fromAttributes == [.top, .leading, .bottom, .trailing])
XCTAssert(toAttributes == [.topMargin, .leadingMargin, .bottomMargin, .trailingMargin])
fromAttributes.removeAll()
toAttributes.removeAll()
view.snp.remakeConstraints { (make) -> Void in
make.directionalMargins.equalTo(self.container.snp.directionalEdges)
}
XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints")
for constraint in (container.snp_constraints as! [NSLayoutConstraint]) {
fromAttributes.insert(constraint.firstAttribute)
toAttributes.insert(constraint.secondAttribute)
}
XCTAssert(toAttributes == [.top, .leading, .bottom, .trailing])
XCTAssert(fromAttributes == [.topMargin, .leadingMargin, .bottomMargin, .trailingMargin])
}
func testLayoutGuideConstraints() { func testLayoutGuideConstraints() {
let vc = UIViewController() let vc = UIViewController()