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

Commit

Permalink
[FEATURE] Added Greater SnapManager Chaining
Browse files Browse the repository at this point in the history
- SnapManager supports greater number of chaining methods
  • Loading branch information
Satinder Singh committed Apr 6, 2017
1 parent 29fcbcc commit b779507
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 11 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.1.0)
- SnapLayout (1.3.0)

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

SPEC CHECKSUMS:
SnapLayout: 3fd4d7b9e43598c6d2140e2827a5ea5791dced05
SnapLayout: 80268468432382a44ba9277365d42b4acb4932d1

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: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ button1.snap(trailingView: button2, constant: 8)
```
These buttons are now side by side where button2 is now the trailingView. No longer will developers have to think which trailing constraint should apply to which leading constraint. This keeps the code lean and clean.

<!-- Chaining ability not setup yet. TODO
### Chaining
```swift
let snapManager = view.snap(top: 8, leading: 8, width: 50)
Expand All @@ -83,7 +82,6 @@ print(snapManager.top?.constant) # 8.0
print(snapManager.height?.constant) # 0.5
```
Snap calls may also be chained and will continue to return a `SnapManager`.
-->
### Constants
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.

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.2.2'
s.version = '1.3.0'
s.summary = 'Concise API for iOS Auto Layout'

s.description = <<-DESC
Expand Down
105 changes: 104 additions & 1 deletion SnapLayout/Classes/SnapManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class SnapManager {
/// - constants: ConstraintConstants to apply
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(to view: UIView? = nil, constants: SnapConfig) -> SnapManager {
public func snap(to view: UIView? = nil, constants: SnapConfig) -> SnapManager {
return snap(to: view,
top: constants.top,
leading: constants.leading,
Expand All @@ -97,4 +97,107 @@ public class SnapManager {
centerY: constants.centerY)
}

/// Apply width anchor between calling view and argument view with specified multiplier
///
/// - Parameters:
/// - view: UIView to apply constraint with (defaulted to superview if nil)
/// - multiplier: Multiplier value to apply constraint with (default 1)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
public func snapWidth(to view: UIView? = nil, multiplier: CGFloat = 1) -> SnapManager {
guard let selfView = selfView else {
print("SnapLayout Error - Cannot apply constraint upon a view that is not retained")
return SnapManager()
}
return selfView.snapWidth(to: view, multiplier: multiplier)
}

/// Apply height anchor between calling view and argument view with specified multiplier
///
/// - Parameters:
/// - view: UIView to apply constraint with (defaulted to superview if nil)
/// - multiplier: Multiplier value to apply constraint with (default 1)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
public func snapHeight(to view: UIView? = nil, multiplier: CGFloat = 1) -> SnapManager {
guard let selfView = selfView else {
print("SnapLayout Error - Cannot apply constraint upon a view that is not retained")
return SnapManager()
}
return selfView.snapHeight(to: view, multiplier: multiplier)
}

/// Anchor size by applying width anchor and height anchor
///
/// - Parameter size: CGSize specifying width and height
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
public func snapSize(size: CGSize) -> SnapManager {
guard let selfView = selfView else {
print("SnapLayout Error - Cannot apply constraint upon a view that is not retained")
return SnapManager()
}
return selfView.snapSize(size: size)
}

/// Applies necessary constraint to ensure calling view will be leading view and the trailingView is on the trailing side.
/// Initalizes trailing property of SnapManager
/// - Parameters:
/// - trailingView: View who will be shown as the trailingView
/// - constant: Constant value to apply constraint with (default 0)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
public func snap(trailingView: UIView, constant: CGFloat = 0) -> SnapManager {
guard let selfView = selfView else {
print("SnapLayout Error - Cannot apply constraint upon a view that is not retained")
return SnapManager()
}
return selfView.snap(trailingView: trailingView, constant: constant)
}

/// Applies necessary constraint to ensure calling view will be trailing and the leadingView is on the leading side.
/// Initalizes trailing property of SnapManager
/// - Parameters:
/// - leadingView: View who will be shown as the leadingView
/// - constant: Constant value to apply constraint with (default 0)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
public func snap(leadingView: UIView, constant: CGFloat = 0) -> SnapManager {
guard let selfView = selfView else {
print("SnapLayout Error - Cannot apply constraint upon a view that is not retained")
return SnapManager()
}
return selfView.snap(leadingView: leadingView, constant: constant)
}

/// Applies necessary constraint to ensure calling view will be top view and the bottom view will be bottom view
/// Initalizes bottom property of SnapManager
/// - Parameters:
/// - bottomView: View who will be shown as the bottomView
/// - constant: Constant value to apply constraint with (default 0)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
public func snap(bottomView: UIView, constant: CGFloat = 0) -> SnapManager {
guard let selfView = selfView else {
print("SnapLayout Error - Cannot apply constraint upon a view that is not retained")
return SnapManager()
}
return selfView.snap(bottomView: bottomView, constant: constant)
}

/// Applies necessary constraint to ensure calling view will be bottom view and the top view will be top view
///
/// - Parameters:
/// - topView: View who will be shown as the bottomView
/// - constant: Constant value to apply constraint with (default 0)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
public func snap(topView: UIView, constant: CGFloat = 0) -> SnapManager {
guard let selfView = selfView else {
print("SnapLayout Error - Cannot apply constraint upon a view that is not retained")
return SnapManager()
}
return selfView.snap(topView: topView, constant: constant)
}

}

0 comments on commit b779507

Please sign in to comment.