Skip to content
This repository has been archived by the owner on Sep 15, 2019. It is now read-only.

Commit

Permalink
[FEATURE] centerX and centerY constraints now support offset
Browse files Browse the repository at this point in the history
- Offset can be applied from centerX and/or centerY
- Updated ReadME with changes
  • Loading branch information
Satinder Singh committed Apr 23, 2017
1 parent 728c58b commit 6e8116b
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- SnapLayout (1.3.0)
- SnapLayout (1.4.1)

DEPENDENCIES:
- SnapLayout (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
SnapLayout: 80268468432382a44ba9277365d42b4acb4932d1
SnapLayout: ac48d267164271e7bbcbf391e29295d384ac0cbb

PODFILE CHECKSUM: f6cd245470fd9f107dd94e8ef9bffeaa3467d0af

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/SnapLayout.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Example/Pods/Target Support Files/SnapLayout/Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Example/SnapLayout/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal final class ViewController: UIViewController {
snapLabel.font = UIFont(name: "Avenir-Book", size: 24)
containerView.addSubview(snapLabel)
// label will be below emojiTextView and aligned horizontally relative to super view
snapLabel.snap(topView: emojiLabel).snap(centerX: true)
snapLabel.snap(topView: emojiLabel).snap(centerX: 0)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Example/SnapLayout_ExampleTests/SnapConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class SnapConfigTests: XCTestCase {
let trailingConstant = CGFloat(24)
let widthConstant = CGFloat(32)
let heightConstant = CGFloat(64)
let centerXConstant = true
let centerYConstant = true
let centerXConstant = CGFloat(0)
let centerYConstant = CGFloat(0)
let snapConfig = SnapConfig(top: topConstant,
leading: leadingConstant,
bottom: bottomConstant,
Expand Down
22 changes: 12 additions & 10 deletions Example/SnapLayout_ExampleTests/SnapManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class SnapManagerTests: BaseTestCase {
let trailingConstant = CGFloat(24)
let widthConstant = CGFloat(32)
let heightConstant = CGFloat(64)
let centerXConstant = true
let centerYConstant = true
let centerXConstant = CGFloat(0)
let centerYConstant = CGFloat(0)
let snapManager = childView.snap(top: topConstant,
leading: leadingConstant,
bottom: bottomConstant,
Expand All @@ -45,9 +45,9 @@ class SnapManagerTests: BaseTestCase {
XCTAssertNotNil(snapManager.height)
XCTAssertEqual(snapManager.height!.constant, heightConstant)
XCTAssertNotNil(snapManager.centerX)
XCTAssertEqual(snapManager.centerX!.constant, 0)
XCTAssertEqual(snapManager.centerX!.constant, centerXConstant)
XCTAssertNotNil(snapManager.centerY)
XCTAssertEqual(snapManager.centerY!.constant, 0)
XCTAssertEqual(snapManager.centerY!.constant, centerYConstant)
}

/// Test Snap Manager Constructor with a view who does not have a superview
Expand Down Expand Up @@ -114,8 +114,8 @@ class SnapManagerTests: BaseTestCase {
let trailingConstant = CGFloat(24)
let widthConstant = CGFloat(32)
let heightConstant = CGFloat(64)
let centerXConstant = true
let centerYConstant = true
let centerXConstant = CGFloat(0)
let centerYConstant = CGFloat(0)
let snapConfig = SnapConfig(top: topConstant,
leading: leadingConstant,
bottom: bottomConstant,
Expand All @@ -139,9 +139,9 @@ class SnapManagerTests: BaseTestCase {
XCTAssertNotNil(snapManager.height)
XCTAssertEqual(snapManager.height!.constant, heightConstant)
XCTAssertNotNil(snapManager.centerX)
XCTAssertEqual(snapManager.centerX!.constant, 0)
XCTAssertEqual(snapManager.centerX!.constant, centerXConstant)
XCTAssertNotNil(snapManager.centerY)
XCTAssertEqual(snapManager.centerY!.constant, 0)
XCTAssertEqual(snapManager.centerY!.constant, centerYConstant)
}

/// Test Snap Manager Config Constructor when used with chaining.
Expand All @@ -153,8 +153,8 @@ class SnapManagerTests: BaseTestCase {
let trailingConstant = CGFloat(24)
let widthConstant = CGFloat(32)
let heightConstant = CGFloat(64)
let centerXConstant = true
let centerYConstant = true
let centerXConstant = CGFloat(0)
let centerYConstant = CGFloat(0)
let snapConfig = SnapConfig(top: topConstant,
leading: leadingConstant,
bottom: bottomConstant,
Expand All @@ -177,7 +177,9 @@ class SnapManagerTests: BaseTestCase {
XCTAssertNotNil(snapManager.height)
XCTAssertEqual(snapManager.height!.constant, heightConstant)
XCTAssertNotNil(snapManager.centerX)
XCTAssertEqual(snapManager.centerX!.constant, centerXConstant)
XCTAssertNotNil(snapManager.centerY)
XCTAssertEqual(snapManager.centerY!.constant, centerYConstant)
}

/// Tests Snap Manager width during chaining
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ Lets use a real world example. Lets say the following view needs to be added. It

```swift
squareView.translatesAutoresizingMaskIntoConstraints = false
squareView.centerXAnchor.constraint(equalTo: squareSuperview.centerXAnchor)
squareView.centerYAnchor.constraint(equalTo: squareSuperview.centerYAnchor)
squareView.widthAnchor.constraint(equalToConstant: 50)
squareView.heightAnchor.constraint(equalToConstant: 50)
squareView.centerXAnchor.constraint(equalTo: squareSuperview.centerXAnchor, constant: 0)
squareView.centerYAnchor.constraint(equalTo: squareSuperview.centerYAnchor, constant: 0)
```

SnapLayout:

```swift
squareView.snap(centerX: true, centerY: true, width: 50, height: 50)
squareView.snap(width: 50, height: 50, centerX: 0, centerY: 0)
```
SnapLayout handles `translatesAutoresizingMaskIntoConstraints` and references the superview of `squareView` when applying constraints. Built to be flexible, yet readable.

## Setup
### Requirements
Expand Down Expand Up @@ -62,7 +63,7 @@ pod "SnapLayout"

### [`UIView`](SnapLayout/Classes/SnapLayout.swift) extension methods
```swift
func snap(to view: UIView? = nil, top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil, trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil, centerX: Bool? = nil, centerY: Bool? = nil) -> ConstraintManager
func snap(to view: UIView? = nil, top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil, trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil, centerX: CGFloat? = nil, centerY: CGFloat? = nil) -> ConstraintManager
func snap(to view: UIView? = nil, constants: SnapConfig) -> SnapManager
func snapWidth(to view: UIView, multiplier: CGFloat = 1) -> SnapManager
func snapHeight(to view: UIView, multiplier: CGFloat = 1) -> SnapManager
Expand Down Expand Up @@ -99,7 +100,7 @@ Snap calls may also be chained and will continue to return a `SnapManager`.
A `SnapConfig ` struct is also available where a developer may list all of their constraint constants beforehand and provide this type to the snap method argument.

```swift
let buttonSnapConfig = SnapConfig(top: 50, leading: 50, trailing: 50, width: 30, centerX: true)
let buttonSnapConfig = SnapConfig(top: 50, leading: 50, trailing: 50, width: 30, centerX: 0)
let buttonSnapConfig = button.snap(constants: buttonConstraintConstants)
```

Expand Down
2 changes: 1 addition & 1 deletion SnapLayout.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SnapLayout'
s.version = '1.4.1'
s.version = '1.5.0'
s.summary = 'Concise API for iOS Auto Layout'

s.description = <<-DESC
Expand Down
14 changes: 7 additions & 7 deletions SnapLayout/Classes/SnapConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public struct SnapConfig {
/// Constant value associated with height constraint. If nil, no constraint is applied.
public var height: CGFloat?

/// Value associated with centerX constraint. Constraint only applied if true.
public var centerX: Bool?
/// Value associated with centerX constraint. If nil, no constraint is applied.
public var centerX: CGFloat?

/// Value associated with centerY constraint. Constraint only applied if true.
public var centerY: Bool?
/// Value associated with centerY constraint. If nil, no constraint is applied.
public var centerY: CGFloat?

/// Constant value associated with zero for top, leading, bottom, and trailing.
/// No width, height, centerX nor centerY constraint is applied.
Expand All @@ -46,11 +46,11 @@ public struct SnapConfig {
/// - trailing: Constant to apply from trailingAnchor (if nil, not applied)
/// - width: Constant value associated with width constraint. If nil, no constraint is applied.
/// - height: Constant to apply from heightAnchor (if nil, not applied)
/// - centerX: Boolean determining if centerX should be applied (if nil, not applied)
/// - centerY: Boolean determining if centerY should be applied (if nil, not applied)
/// - centerX: Constant offset to apply from centerXAnchor (if nil, not applied)
/// - centerY: Constant offset to apply from centerXAnchor (if nil, not applied)
public init(top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil,
trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil,
centerX: Bool? = nil, centerY: Bool? = nil) {
centerX: CGFloat? = nil, centerY: CGFloat? = nil) {
self.top = top
self.leading = leading
self.bottom = bottom
Expand Down
16 changes: 8 additions & 8 deletions SnapLayout/Classes/SnapLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public extension UIView {
/// - trailing: Constant to apply from trailingAnchor (if nil, not applied)
/// - width: Constant to apply from widthAnchor (if nil, not applied)
/// - height: Constant to apply from heightAnchor (if nil, not applied)
/// - centerX: Boolean determining if centerX should be applied (if nil, not applied)
/// - centerY: Boolean determining if centerY should be applied (if nil, not applied)
/// - centerX: Constant offset to apply from centerXAnchor (if nil, not applied)
/// - centerY: Constant offset to apply from centerXAnchor (if nil, not applied)
/// - Note: width and height are not in respect to superview, but always to self
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(to view: UIView? = nil, top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil,
trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil, centerX: Bool? = nil,
centerY: Bool? = nil) -> SnapManager {
trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil, centerX: CGFloat? = nil,
centerY: CGFloat? = nil) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
var snapManager = SnapManager(view: self)
if let width = width {
Expand Down Expand Up @@ -61,12 +61,12 @@ public extension UIView {
snapManager.trailing = view.trailingAnchor.constraint(equalTo: trailingAnchor, constant: trailing)
snapManager.trailing?.isActive = true
}
if let centerX = centerX, centerX {
snapManager.centerX = centerXAnchor.constraint(equalTo: view.centerXAnchor)
if let centerX = centerX {
snapManager.centerX = centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: centerX)
snapManager.centerX?.isActive = true
}
if let centerY = centerY, centerY {
snapManager.centerY = centerYAnchor.constraint(equalTo: view.centerYAnchor)
if let centerY = centerY {
snapManager.centerY = centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: centerY)
snapManager.centerY?.isActive = true
}
let inActiveCount = [snapManager.width, snapManager.height, snapManager.top, snapManager.leading,
Expand Down
4 changes: 2 additions & 2 deletions SnapLayout/Classes/SnapManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public class SnapManager {
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
public func snap(to view: UIView? = nil, top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil,
trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil, centerX: Bool? = nil,
centerY: Bool? = nil) -> SnapManager {
trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil, centerX: CGFloat? = nil,
centerY: CGFloat? = nil) -> SnapManager {
guard let weakView = weakView else {
print("SnapLayout Error - Cannot apply constraint upon a view that is not retained")
return SnapManager()
Expand Down

0 comments on commit 6e8116b

Please sign in to comment.