This repository has been archived by the owner on Sep 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapLayout.swift
234 lines (223 loc) · 12.5 KB
/
SnapLayout.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// SnapLayout.swift
// Pods
//
// Created by Satinder Singh on 10/2/16.
//
//
/// Extension Methods for convenience
public extension View {
/// Snap to view based on argument values.
/// - Warning: Since all parameters have default values, it is possible to call `snap()` with no parameters.
/// This is **strongly** discouraged.
/// - Parameters:
/// - view: View to to apply constraints with (defaulted to superview if nil)
/// - top: Constant to apply to top constraint from topAnchor (if nil, not applied)
/// - leading: Constant to apply from leadingAnchor (if nil, not applied)
/// - bottom: Constant to apply from bottomAnchor (if nil, not applied)
/// - 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: Constant offset to apply from centerXAnchor (if nil, not applied)
/// - centerY: Constant offset to apply from centerXAnchor (if nil, not applied)
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - 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: View? = 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, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
var snapManager = SnapManager(view: self)
var constraintList = [NSLayoutConstraint]()
defer {
constraintList.forEach { $0.priority = priority }
if constraintList.count == 0 {
Swift.print("SnapLayout Error - No constraint was applied for view: \(String(describing: view))")
} else if isActive {
NSLayoutConstraint.activate(constraintList)
}
}
if let width = width {
let constraint = os_anchors.widthAnchor.constraint(equalToConstant: width)
snapManager.width = constraint
constraintList.append(constraint)
}
if let height = height {
let constraint = os_anchors.heightAnchor.constraint(equalToConstant: height)
snapManager.height = constraint
constraintList.append(constraint)
}
guard let view = view ?? superview else {
return snapManager
}
if let top = top {
let constraint = topAnchor.constraint(equalTo: view.os_anchors.topAnchor, constant: top)
snapManager.top = constraint
constraintList.append(constraint)
}
if let leading = leading {
let constraint = leadingAnchor.constraint(equalTo: view.os_anchors.leadingAnchor, constant: leading)
snapManager.leading = constraint
constraintList.append(constraint)
}
if let bottom = bottom {
let constraint = view.os_anchors.bottomAnchor.constraint(equalTo: bottomAnchor, constant: bottom)
snapManager.bottom = constraint
constraintList.append(constraint)
}
if let trailing = trailing {
let constraint = view.os_anchors.trailingAnchor.constraint(equalTo: trailingAnchor, constant: trailing)
snapManager.trailing = constraint
constraintList.append(constraint)
}
if let centerX = centerX {
let constraint = centerXAnchor.constraint(equalTo: view.os_anchors.centerXAnchor, constant: centerX)
snapManager.centerX = constraint
constraintList.append(constraint)
}
if let centerY = centerY {
let constraint = centerYAnchor.constraint(equalTo: view.os_anchors.centerYAnchor, constant: centerY)
snapManager.centerY = constraint
constraintList.append(constraint)
}
return snapManager
}
/// Snap to view based on argument values.
/// Nil properties within ConstraintConstants will not apply constraints.
///
/// - Parameters:
/// - view: View to to apply constraints with (defaulted to superview if nil)
/// - config: SnapConfig to apply
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(to view: View? = nil, config: SnapConfig, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
return snap(to: view,
top: config.top,
leading: config.leading,
bottom: config.bottom,
trailing: config.trailing,
width: config.width,
height: config.height,
centerX: config.centerX,
centerY: config.centerY,
priority: priority,
isActive: isActive)
}
/// Apply width anchor between calling view and argument view with specified multiplier
///
/// - Parameters:
/// - view: View to apply constraint with (defaulted to superview if nil)
/// - multiplier: Multiplier value to apply constraint with (default 1)
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snapWidth(to view: View, multiplier: CGFloat = 1, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
let snapManager = SnapManager(view: self)
snapManager.width = widthAnchor.constraint(equalTo: view.os_anchors.widthAnchor, multiplier: multiplier)
snapManager.width?.priority = priority
snapManager.width?.isActive = isActive
return snapManager
}
/// Apply height anchor between calling view and argument view with specified multiplier
///
/// - Parameters:
/// - view: View to apply constraint with (defaulted to superview if nil)
/// - multiplier: Multiplier value to apply constraint with (default 1)
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snapHeight(to view: View, multiplier: CGFloat = 1, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
let snapManager = SnapManager(view: self)
snapManager.height = heightAnchor.constraint(equalTo: view.os_anchors.heightAnchor, multiplier: multiplier)
snapManager.height?.priority = priority
snapManager.height?.isActive = isActive
return snapManager
}
/// Anchor size by applying width anchor and height anchor
///
/// - Parameters:
/// - size: CGSize specifying width and height
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(size: CGSize, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
return snap(width: size.width, height: size.height, priority: priority, isActive: isActive)
}
/// 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)
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(trailingView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
let snapManager = SnapManager(view: self)
snapManager.trailing = trailingView.os_anchors.leadingAnchor.constraint(equalTo: trailingAnchor, constant: constant)
snapManager.trailing?.priority = priority
snapManager.trailing?.isActive = isActive
return snapManager
}
/// 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)
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(leadingView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
let snapManager = SnapManager(view: self)
snapManager.leading = leadingAnchor.constraint(equalTo: leadingView.os_anchors.trailingAnchor, constant: constant)
snapManager.leading?.priority = priority
snapManager.leading?.isActive = isActive
return snapManager
}
/// 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)
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(bottomView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
let snapManager = SnapManager(view: self)
snapManager.bottom = bottomView.os_anchors.topAnchor.constraint(equalTo: bottomAnchor, constant: constant)
snapManager.bottom?.priority = priority
snapManager.bottom?.isActive = isActive
return snapManager
}
/// 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)
/// - priority: LayoutPriority to apply upon constraint (default required)
/// - isActive: Boolean determining if constraint should be activated (default true)
/// - Returns: SnapManager holding all the values associated with constraints
@discardableResult
func snap(topView: View, constant: CGFloat = 0, priority: LayoutPriority = LayoutPriorityRequired, isActive: Bool = true) -> SnapManager {
translatesAutoresizingMaskIntoConstraints = false
let snapManager = SnapManager(view: self)
snapManager.top = topAnchor.constraint(equalTo: topView.os_anchors.bottomAnchor, constant: constant)
snapManager.top?.priority = priority
snapManager.top?.isActive = isActive
return snapManager
}
}