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

Commit 70bd4a6

Browse files
author
Satinder Singh
committed
Updated API Interface
- Renamed Methods - Removed Unnecessary structs - Greater flexibility of options with more default parameters
1 parent 4bfc7ea commit 70bd4a6

File tree

3 files changed

+142
-263
lines changed

3 files changed

+142
-263
lines changed

Example/SnapLayout/ViewController.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SnapLayout
1212
internal final class ViewController: UIViewController {
1313

1414
struct Constants {
15-
static let emojiLabelEdgeInsets = ConstraintEdgeInsets(top: 50, leading: 8, trailing: -8)
15+
static let emojiLabelConstraintConstants = ConstraintConstants(top: 50, leading: 8, trailing: -8)
1616
}
1717

1818
/// Container view encompassing all subviews of View Controller
@@ -32,14 +32,14 @@ internal final class ViewController: UIViewController {
3232
containerView.backgroundColor = UIColor(red: 219/255, green: 165/255, blue: 182/255, alpha: 1)
3333
view.addSubview(containerView)
3434
// Container View will now encompass entire view real estate
35-
containerView.pinToSuperview(.zero)
35+
containerView.snap(constants: .zero)
3636
}
3737

3838
/// Setup Container View
3939
fileprivate func setupEmojiLabel() {
4040
emojiLabel.text = emojiList()
4141
containerView.addSubview(emojiLabel)
42-
emojiLabel.pinToSuperview(Constants.emojiLabelEdgeInsets)
42+
emojiLabel.snap(constants: Constants.emojiLabelConstraintConstants)
4343
emojiLabel.numberOfLines = 0
4444
}
4545

@@ -52,7 +52,7 @@ internal final class ViewController: UIViewController {
5252
containerView.addSubview(snapLabel)
5353
// label will be below emojiTextView and aligned horizontally relative to super view
5454
snapLabel.snapVertically(topView: emojiLabel)
55-
snapLabel.alignToSuperView(.centerX)
55+
snapLabel.snap(centerX: true)
5656
}
5757

5858
/**

README.md

+33-47
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
[![License](https://img.shields.io/cocoapods/l/SnapLayout.svg?style=flat)](http://cocoapods.org/pods/SnapLayout)
66
[![Platform](https://img.shields.io/cocoapods/p/SnapLayout.svg?style=flat)](http://cocoapods.org/pods/SnapLayout)
77

8-
Concise API for iOS Auto Layout. SnapLayout extends `UIView` to deliver a list of APIs to improve readability while also shortening constraint code. Internally using AutoLayout to provide the best experience. With SnapLayout, developers can pin, anchor, snap, or align views easily!
8+
Concise API for iOS Auto Layout. SnapLayout extends `UIView` to deliver a list of APIs to improve readability while also shortening constraint code. Internally uses AutoLayout to provide the best experience. With SnapLayout, developers can remove boilerplate code but not at the cost of readability.
9+
10+
Imagine applying any or all of the following constraints in one line of code: top, leading, trailing, bottom, width, height, centerX, centerY. This is possible with `SnapLayout`.
911

1012
### Table of Contents
11-
1. [Setup](#setup)
12-
* [Requirements](#requirements)
13-
* [Installation](#installation)
14-
1. [Usage](#usage)
15-
* [Sample Code](#sample-code)
16-
* [Example App](#example-app)
13+
1. [Setup](#setup)
14+
* [Requirements](#requirements)
15+
* [Installation](#installation)
16+
2. [Usage](#usage)
17+
* [Sample Code](#sample-Code)
18+
* [Example App](#example-app)
1719

1820
## Setup
1921
### Requirements
20-
SnapLayout's current release supports iOS >= 9.0
21-
2222
* Xcode
2323
* Language Support: **Swift** *(3.0)*
2424
* Fully Compatible With: **Xcode 8.0 and higher**
@@ -36,64 +36,50 @@ it, simply add the following line to your Podfile:
3636
pod "SnapLayout"
3737
```
3838

39+
## Overview
40+
SnapLayout offers many `UIView` extension methods available in the [source files](SnapLayout/SnapLayout/Classes/SnapLayout.swift).
41+
42+
* All methods are prefixed with `snap` for quick Xcode autocomplete.
43+
* Directly uses NSLayoutAnchor under the hood so the API is developer friendly
44+
* Any view using `SnapLayout` will not only have its `translatesAutoresizingMaskIntoConstraints` set to false, but also have its constraint activated.
45+
* Amazing constraint situations such as snapping a button to the label on top of it is an effortless process now: `button.snapVertically(topView: label, constant: 8)`
46+
3947
## Usage
4048
### [`UIView`](SnapLayout/Classes/SnapLayout.swift) extension methods
4149
```swift
42-
func pinToSuperview(top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil, trailing: CGFloat? = nil) -> ConstraintManager
43-
func pinToSuperview(_ insets: ConstraintEdgeInsets) -> ConstraintManager
44-
func pin(view: UIView, top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil, trailing: CGFloat? = nil) -> ConstraintManager
45-
func pin(view: UIView, insets: ConstraintEdgeInsets) -> ConstraintManager
46-
func anchorWidth(constant: CGFloat) -> ConstraintManager
47-
func anchorWidth(view: UIView, multiplier: CGFloat = 1, constant: CGFloat = 0) -> ConstraintManager
48-
func anchorWidthToSuperview(multiplier: CGFloat = 1, constant: CGFloat = 0) -> ConstraintManager
49-
func anchorHeight(constant: CGFloat = 0) -> ConstraintManager
50-
func anchorHeight(view: UIView, multiplier: CGFloat = 1, constant: CGFloat = 0) -> ConstraintManager
51-
func anchorHeightToSuperview(multiplier: CGFloat = 0, constant: CGFloat = 0) -> ConstraintManager
52-
func anchorSizeToSuperView(_ size: CGSize) -> ConstraintManager
53-
func anchorSize(view: UIView) -> ConstraintManager
54-
func anchorSize(size: CGSize) -> ConstraintManager
50+
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)
51+
func snap(to view: UIView? = nil, constants: ConstraintConstants) -> ConstraintManager
52+
func snapWidth(constant: CGFloat) -> ConstraintManager
53+
func snapWidth(to view: UIView? = nil, multiplier: CGFloat = 1) -> ConstraintManager
54+
func snapHeight(constant: CGFloat) -> ConstraintManager
55+
func snapHeight(to view: UIView? = nil, multiplier: CGFloat = 1) -> ConstraintManager
56+
func snapSize(size: CGSize) -> ConstraintManager
5557
func snapHorizontally(trailingView: UIView, constant: CGFloat = 0) -> ConstraintManager
5658
func snapHorizontally(leadingView: UIView, constant: CGFloat = 0) -> ConstraintManager
5759
func snapVertically(bottomView: UIView, constant: CGFloat = 0) -> ConstraintManager
5860
func snapVertically(topView: UIView, constant: CGFloat = 0) -> ConstraintManager
59-
func align(_ axis: ConstraintAxis, toView: UIView, offset: UIOffset = .zero) -> ConstraintManager
60-
func alignToSuperView(_ axis: ConstraintAxis, offset: UIOffset = .zero) -> ConstraintManager
6161
```
6262

6363
### Sample Code
6464

6565
```swift
66-
view.pinToSuperview(.zero)
67-
view.pinToSuperview(ConstraintEdgeInsets(top: 50, leading: 8))
68-
view.anchorHeightToSuperview()
69-
view.anchorHeightToSuperview(multiplier: 0.5)
70-
view.anchorHeightToSuperview(multiplier: 0.5, constant: 20)
71-
view.anchorWidthToSuperview(constant: 20)
72-
view.anchorSizeToSuperview(size: CGSize(width: 40, height:60))
73-
view.anchorWidth(20)
74-
view.anchorHeight(30)
75-
view.anchorSize(CGSize(width: 20, height:30))
76-
view.snapHorizontally(trailingView: view2)
77-
view.snapHorizontally(trailingView: view2, constant: 8)
78-
view.snapVertically(bottomView: view2, constant: 8)
79-
view.snapVertically(topView: view2)
80-
view.align(.centerX, toView: view2)
81-
view.align(.centerXAndY, toView: view2, offset: UIOffset(horizontal: 10, vertical: 5))
82-
view.alignToSuperView(.centerY)
83-
view.alignToSuperView(.centerY, offset: UIOffset(horizontal: 0, vertical: 5))
66+
let buttonConstraintManager = button.snap(top: 50, leading: 50, trailing: 50, width: 30)
67+
buttonConstraintManager.top?.constant = 100
8468
```
69+
Not only has this button applied 4 constraints, but each individual constraint is accessible through the returned `ConstraintManager`. The beauty of SnapLayout is not only its power interface, but how easy it is to adjust returned constraints. Other API's simply return an array, but not SnapLayout. Each constraint is neatly packaged into a `ConstraintManager`.
8570

86-
### Notes
87-
All of the UIView Extensions return a `ConstraintManager`. This holds all of the applied constraints. Imagine a scenario where a view has a height constraint and pinned to the top, leading, and trailing of super view. Both of these constraints exist within their respective `ConstraintManager`. This manager also has a handy `append` function which combines another constraint manager with itself. In the following example, `loginConstraintManager` holds many initalized properties so a developer can quickly access the `height`, `top`, `leading`, and `trailing` constraints through `loginConstraintManager `.
71+
### Constants
72+
A `ConstraintConstants` struct is also available where a developer may list all of their constraint constants beforehand and provide a `ConstraintConstants ` struct to the snap method argument.
8873

8974
```swift
90-
let loginConstraintManager = loginButton.anchorHeightToSuperview()
91-
loginConstraintManager.append(loginButton.pinToSuperview(top: 8, leading: 8, trailing: 8))
75+
let buttonConstraintConstants = ConstraintConstants(top: 50, leading: 50, trailing: 50, width: 30)
76+
let buttonConstraintManager = button.snap(constants: buttonConstraintConstants)
77+
buttonConstraintManager.top?.constant = 100
9278
```
9379

9480
### Example App
9581

96-
To run the example project, clone the repo, and run `pod install` from the Example directory first.
82+
To run the example project, run `pod try SnapLayout`
9783

9884
## Author
9985

0 commit comments

Comments
 (0)