mirror of https://github.com/SnapKit/SnapKit
Merge branch 'gh-pages' of github.com:SnapKit/SnapKit into gh-pages
This commit is contained in:
commit
b0fefc5741
80
docs.md
80
docs.md
|
@ -96,7 +96,7 @@ let box = UIView()
|
|||
superview.addSubview(box)
|
||||
|
||||
box.snp.makeConstraints { (make) -> Void in
|
||||
make.edges.equalTo(superview).inset(UIEdgeInsetsMake(20, 20, 20, 20))
|
||||
make.edges.equalTo(superview).inset(UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -104,15 +104,15 @@ Not only does this greatly shorten and increase the readability of constraints S
|
|||
|
||||
- Determining the best common superview to install the constraints on.
|
||||
- Keeping track of the constrainted installed so they can easily be removed later.
|
||||
- Ensuring `setTranslatesAutoresizingMaskIntoConstraints(false)` is called on all appropriate views.
|
||||
- Ensuring `translatesAutoresizingMaskIntoConstraints = false` is called on all appropriate views.
|
||||
|
||||
### Not all things are created equal
|
||||
|
||||
> `.equalTo` equivalent to **NSLayoutRelation.Equal**
|
||||
> `.equalTo` equivalent to **NSLayoutConstraint.Relation.equal**
|
||||
|
||||
> `.lessThanOrEqualTo` equivalent to **NSLayoutRelation.LessThanOrEqual**
|
||||
> `.lessThanOrEqualTo` equivalent to **NSLayoutConstraint.Relation.lessThanOrEqual**
|
||||
|
||||
> `.greaterThanOrEqualTo` equivalent to **NSLayoutRelation.GreaterThanOrEqual**
|
||||
> `.greaterThanOrEqualTo` equivalent to **NSLayoutConstraint.Relation.greaterThanOrEqual**
|
||||
|
||||
These three equality constraints accept one argument which can be any of the following:
|
||||
|
||||
|
@ -124,17 +124,17 @@ make.centerX.lessThanOrEqualTo(view2.snp.left)
|
|||
|
||||
ViewAttribute | NSLayoutAttribute
|
||||
------------------------- | --------------------------
|
||||
view.snp.left | NSLayoutAttribute.left
|
||||
view.snp.right | NSLayoutAttribute.right
|
||||
view.snp.top | NSLayoutAttribute.top
|
||||
view.snp.bottom | NSLayoutAttribute.bottom
|
||||
view.snp.leading | NSLayoutAttribute.leading
|
||||
view.snp.trailing | NSLayoutAttribute.trailing
|
||||
view.snp.width | NSLayoutAttribute.width
|
||||
view.snp.height | NSLayoutAttribute.height
|
||||
view.snp.centerX | NSLayoutAttribute.centerX
|
||||
view.snp.centerY | NSLayoutAttribute.centerY
|
||||
view.snp.lastBaseline | NSLayoutAttribute.lastBaseline
|
||||
view.snp.left | NSLayoutConstraint.Attribute.left
|
||||
view.snp.right | NSLayoutConstraint.Attribute.right
|
||||
view.snp.top | NSLayoutConstraint.Attribute.top
|
||||
view.snp.bottom | NSLayoutConstraint.Attribute.bottom
|
||||
view.snp.leading | NSLayoutConstraint.Attribute.leading
|
||||
view.snp.trailing | NSLayoutConstraint.Attribute.trailing
|
||||
view.snp.width | NSLayoutConstraint.Attribute.width
|
||||
view.snp.height | NSLayoutConstraint.Attribute.height
|
||||
view.snp.centerX | NSLayoutConstraint.Attribute.centerX
|
||||
view.snp.centerY | NSLayoutConstraint.Attribute.centerY
|
||||
view.snp.lastBaseline | NSLayoutConstraint.Attribute.lastBaseline
|
||||
|
||||
|
||||
#### 2. UIView/NSView
|
||||
|
@ -180,12 +180,18 @@ make.left.equalTo(view).offset(UIEdgeInsets(top: 10, left: 0, bottom: 10, right:
|
|||
|
||||
> `.priority` allows you to specify an exact priority
|
||||
|
||||
Priorities are can be tacked on to the end of a constraint chain like so:
|
||||
Priorities can be tacked on to the end of a constraint chain like so:
|
||||
|
||||
```swift
|
||||
make.top.equalTo(label.snp.top).priority(600)
|
||||
```
|
||||
|
||||
You may also use priority shortcuts: `.low`, `.medium`, `.high`, `.required`.
|
||||
|
||||
```swift
|
||||
make.top.equalTo(label.snp.top).priority(.medium)
|
||||
```
|
||||
|
||||
### Composition, composition, composition
|
||||
|
||||
SnapKit also gives you a few convenience methods to create multiple constraints at the same time.
|
||||
|
@ -251,7 +257,7 @@ view1.snp.makeConstraints { (make) -> Void in
|
|||
|
||||
...
|
||||
// then later you can call
|
||||
self.topConstraint.uninstall()
|
||||
self.topConstraint.deactivate()
|
||||
|
||||
// or if you want to update the constraint
|
||||
self.topConstraint.updateOffset(5)
|
||||
|
@ -298,9 +304,10 @@ func changeButtonPosition() {
|
|||
}
|
||||
```
|
||||
|
||||
### Snap view to topLayoutGuide and bottomLayoutGuide
|
||||
|
||||
`topLayoutGuide.snp.bottom` is similar to `topLayoutGuide.bottomAnchor` and you can also use `bottomLayoutGuide.snp.top` to align view on top of UITabBar.
|
||||
### Snap view to safeAreaLayoutGuide
|
||||
|
||||
`topLayoutGuide` and `bottomLayoutGuide` were deprecated in `iOS 11`. Use `safeAreaLayoutGuide`:
|
||||
|
||||
```swift
|
||||
import SnapKit
|
||||
|
@ -314,38 +321,29 @@ class MyViewController: UIVewController {
|
|||
|
||||
self.view.addSubview(tableView)
|
||||
tableView.snp.makeConstraints { (make) -> Void in
|
||||
make.top.equalTo(self.topLayoutGuide.snp.bottom)
|
||||
make.left.equalTo(view)
|
||||
make.right.equalTo(view)
|
||||
make.bottom.equalTo(self.bottomLayoutGuide.snp.top)
|
||||
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Debug with ease
|
||||
> `.labeled` allows you to specify constraint labels for debug logs
|
||||
|
||||
### Snap view to safe layout guide
|
||||
|
||||
Just like `topLayoutGuide` and `bottomLayoutGuide` using iPhone X's new `safeAreaLayoutGuide` is easy:
|
||||
Labels can be tacked on to the end of a constraint chain like so:
|
||||
|
||||
```swift
|
||||
import SnapKit
|
||||
|
||||
class MyViewController: UIVewController {
|
||||
|
||||
lazy var tableView = UITableView()
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
self.view.addSubview(tableView)
|
||||
tableView.snp.makeConstraints { (make) -> Void in
|
||||
make.top.equalTo(self.safeAreaLayoutGuide.snp.bottom)
|
||||
}
|
||||
button.snp.makeConstraints { (make) -> Void in
|
||||
make.top.equalTo(otherView).labeled("buttonViewTopConstraint")
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
Resulting `Unable to simultaneously satisfy constraints.` logs will use constraint labels to clearly identify which constraints need attention:
|
||||
|
||||
```
|
||||
"<SnapKit.LayoutConstraint:buttonViewTopConstraint@SignUpViewController.swift#311
|
||||
UIView:0x7fd98491e4c0.leading == UIView:0x7fd983633880.leading>"
|
||||
```
|
||||
|
||||
<br/>
|
||||
|
|
Loading…
Reference in New Issue