-
Notifications
You must be signed in to change notification settings - Fork 10
/
DistributiveConstraintBuilder.swift
102 lines (88 loc) · 4.82 KB
/
DistributiveConstraintBuilder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// DistributiveConstraintBuilder.swift
// SwiftAutoLayout
//
// Created by Jake Sawyer on 11/13/18.
// Copyright © 2018 SwiftKick Mobile. All rights reserved.
//
// Look at all this code you didn't have to write!
import Foundation
import UIKit
/**
A constraint builder for laying out a `Constrainable` horizontally or vertically after another.
*/
public class DistributiveConstraintBuilder: ConstraintBuilder {
/// The constraints created by this builder, in the order they were made.
public internal(set) var constraints = [NSLayoutConstraint]()
let beforeConstrainable: Constrainable
let afterConstrainable: Constrainable
/**
Supplied constrainables, if they are `UIView`s, will have their `translatesAutoresizingMaskIntoConstraints` set to false.
*/
init(before beforeConstrainable: Constrainable, after afterConstrainable: Constrainable) {
self.beforeConstrainable = beforeConstrainable
self.afterConstrainable = afterConstrainable
for view in [beforeConstrainable, afterConstrainable].compactMap({ $0 as? UIView }) {
view.translatesAutoresizingMaskIntoConstraints = false
}
}
}
// MARK: - Make Constraints
public extension DistributiveConstraintBuilder {
/**
Horizontally distribute the before and after constrainables via their leading and trailing anchors.
- NOTE: Creates a single constraint.
- Parameters:
- relation: The relationship between the anchors. Defaults to `.equal`
- constant: The constant for the constraint. Defaults to 0
- multiplier: The multiplier for the constraint. Defaults to 1
- priority: The priority for the constraint. Defaults to `.required`. Note that `.required` constraints must be activated and cannot be deactivated. Specify a lower value for dynamically activated constraints.
- activate: Whether or not this constraint should be activated now. Disable this if you want to activate the constraint later.
- Returns: The constraint builder. Access any constraints built so far in the order declared via its `constraints` property.
*/
@discardableResult
func leadingTrailing(_ relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat = 0,
multiplier: CGFloat = 1,
priority: UILayoutPriority = .required,
activate: Bool = true) -> DistributiveConstraintBuilder {
let constraint = makeConstraint(first: beforeConstrainable.trailingAnchorInfo,
second: afterConstrainable.leadingAnchorInfo,
relation: relation,
constant: constant,
multiplier: multiplier,
priority: priority,
activate: activate,
inverse: true)
constraints.append(constraint)
return self
}
/**
Vertically distribute the before and after constrainables via their top and bottom anchors.
- NOTE: Creates a single constraint.
- Parameters:
- relation: The relationship between the anchors. Defaults to `.equal`
- constant: The constant for the constraint. Defaults to 0
- multiplier: The multiplier for the constraint. Defaults to 1
- priority: The priority for the constraint. Defaults to `.required`. Note that `.required` constraints must be activated and cannot be deactivated. Specify a lower value for dynamically activated constraints.
- activate: Whether or not this constraint should be activated now. Disable this if you want to activate the constraint later.
- Returns: The constraint builder. Access any constraints built so far in the order declared via its `constraints` property.
*/
@discardableResult
func topBottom(_ relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat = 0,
multiplier: CGFloat = 1,
priority: UILayoutPriority = .required,
activate: Bool = true) -> DistributiveConstraintBuilder {
let constraint = makeConstraint(first: beforeConstrainable.bottomAnchorInfo,
second: afterConstrainable.topAnchorInfo,
relation: relation,
constant: constant,
multiplier: multiplier,
priority: priority,
activate: activate,
inverse: true)
constraints.append(constraint)
return self
}
}