Skip to content

Commit 131fb8f

Browse files
add documentation to the methods
1 parent d18005b commit 131fb8f

File tree

4 files changed

+193
-21
lines changed

4 files changed

+193
-21
lines changed

Sources/AutoLayout/LayoutConstraints+Helpers.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
11
extension LayoutConstraints {
2+
/// Merges multiple ``LayoutConstraints`` into a single ``LayoutConstraints``.
3+
///
4+
/// Might be useful when grouping constraints of the same view.
5+
///
6+
/// ```swift
7+
/// let constraints = LayoutConstraints.merged {
8+
/// view.horizontally(16)
9+
/// view.centerVertically()
10+
/// }
11+
/// ```
12+
///
13+
/// > Note: If two or more elements have the same non-nil constraint, the result will contain the constraint from the latest element.
14+
/// - Parameter constraints: Constraints that should be merged.
15+
/// - Returns: An instance that contains constraints from the all provided ``LayoutConstraints``.
216
public static func merged(@LayoutConstraintsBuilder _ constraints: () -> LayoutConstraints) -> LayoutConstraints {
317
return constraints()
418
}
519
}
620

721
extension LayoutConstraints {
22+
/// Merges the provided ``LayoutConstraints`` with a current instance.
23+
///
24+
/// > Note: If the current instance and the provided instance have the same non-nil constraint, the constraint in the current instance will be overridden with a constraint from the provided instance.
25+
/// - Parameter constraint: ``LayoutConstraints`` that should be merged with the current instance.
826
public mutating func merge(with constraint: LayoutConstraints) {
927
self = LayoutConstraints.merged {
1028
self
1129
constraint
1230
}
1331
}
14-
32+
33+
/// Merges the provided constraints with a current instance.
34+
///
35+
/// Might be useful when grouping existing constraints with new values.
36+
///
37+
/// ```swift
38+
/// var constraints = view.leading(10)
39+
/// ...
40+
/// constraints.merge(with: {
41+
/// view.top(10)
42+
/// view.bottom(20)
43+
/// })
44+
/// ```
45+
///
46+
/// > Note: If two or more elements have the same non-nil constraint, the current instance will contain the constraint from the latest element.
47+
/// - Parameter constraints: Constraints that should be merged with the current instance.
1548
public mutating func merge(@LayoutConstraintsBuilder with constraints: () -> LayoutConstraints) {
1649
self = LayoutConstraints.merged {
1750
self

Sources/AutoLayout/LayoutConstraints.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
import UIKit
22

3-
/// Contains constraints of the view.
3+
/// A structure that holds references to various layout constraints applied to a view.
4+
///
5+
/// The `LayoutConstraints` struct is used to store the constraints created by layout helper methods. Each property corresponds to a specific type of constraint that has been applied.
6+
///
7+
/// This struct allows you to easily access and manipulate specific constraints after they have been applied to a view, facilitating adjustments or deactivations as needed.
8+
///
9+
/// ```swift
10+
/// let constraints = myView.edges(16)
11+
/// constraints.top?.constant = 20 // Adjust the top constraint's constant
12+
/// constraints.trailing?.isActive = false // Deactivate the trailing constraint
13+
/// ```
414
public struct LayoutConstraints {
5-
/// Top constraint.
15+
/// The top edge constraint.
616
public var top: NSLayoutConstraint?
7-
/// Leading constraint.
17+
/// The leading edge constraint.
818
public var leading: NSLayoutConstraint?
9-
/// Bottom constraint.
19+
/// The bottom edge constraint.
1020
public var bottom: NSLayoutConstraint?
11-
/// Trailing constraint.
21+
/// The trailing edge constraint.
1222
public var trailing: NSLayoutConstraint?
13-
/// Vertical constraint.
23+
/// The vertical alignment constraint (e.g., center Y).
1424
public var vertical: NSLayoutConstraint?
15-
/// Horizontal constraint.
25+
/// The horizontal alignment constraint (e.g., center X).
1626
public var horizontal: NSLayoutConstraint?
17-
/// Width constraint.
27+
/// The width constraint.
1828
public var width: NSLayoutConstraint?
19-
/// Height constraint.
29+
/// The height constraint.
2030
public var height: NSLayoutConstraint?
2131

22-
/// Array of all constraints.
32+
/// Array with all constraints.
2333
public var allConstraints: [NSLayoutConstraint?] {
2434
return [
2535
self.top,
@@ -54,12 +64,12 @@ public struct LayoutConstraints {
5464
self.height = height
5565
}
5666

57-
/// Activates existing constraints.
67+
/// Activates all existing constraints.
5868
public func activateAll() {
5969
self.allConstraints.forEach { $0?.isActive = true }
6070
}
6171

62-
/// Deactivates existing constraints.
72+
/// Deactivates all existing constraints.
6373
public func deactivateAll() {
6474
self.allConstraints.forEach { $0?.isActive = false }
6575
}

Sources/AutoLayout/LayoutConstraintsBuilder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/// A result builder for merging multiple ``LayoutConstraints`` into a single ``LayoutConstraints``.
2+
///
3+
/// NOTE: If two elements have the same non-nil constraint, the result will contain the constraint of the latest element.
14
@resultBuilder
25
public struct LayoutConstraintsBuilder {
36
public static func buildBlock(_ constraints: LayoutConstraints...) -> LayoutConstraints {

Sources/AutoLayout/UIView+Layout.swift

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import UIKit
22

33
extension UIView {
4+
/// Constrains the view's leading anchor to the leading anchor of another view or its superview.
5+
///
6+
/// - Parameters:
7+
/// - padding: The distance between the leading edges of the two views.
8+
/// - view: The view to which the leading edge will be constrained. If `nil`, the view's superview is used.
9+
/// - safeArea: A Boolean indicating whether to constrain to the `safeAreaLayoutGuide` of the other view. If `true`, the constraint respects the safe area.
10+
///
11+
/// - Returns: A ``LayoutConstraints`` object containing the leading edge constraint.
412
@discardableResult
513
public func leading(
614
_ padding: CGFloat = 0,
@@ -25,6 +33,14 @@ extension UIView {
2533
return LayoutConstraints(leading: constraint)
2634
}
2735

36+
/// Constrains the view's trailing anchor to the trailing anchor of another view or its superview.
37+
///
38+
/// - Parameters:
39+
/// - padding: The distance between the trailing edges of the two views.
40+
/// - view: The view to which the trailing edge will be constrained. If `nil`, the view's superview is used.
41+
/// - safeArea: A Boolean indicating whether to constrain to the `safeAreaLayoutGuide` of the other view. If `true`, the constraint respects the safe area.
42+
///
43+
/// - Returns: A ``LayoutConstraints`` object containing the trailing edge constraint.
2844
@discardableResult
2945
public func trailing(
3046
_ padding: CGFloat = 0,
@@ -49,6 +65,14 @@ extension UIView {
4965
return LayoutConstraints(trailing: constraint)
5066
}
5167

68+
/// Constrains the view's top anchor to the top anchor of another view or its superview.
69+
///
70+
/// - Parameters:
71+
/// - padding: The distance between the top edges of the two views.
72+
/// - view: The view to which the top edge will be constrained. If `nil`, the view's superview is used.
73+
/// - safeArea: A Boolean indicating whether to constrain to the `safeAreaLayoutGuide` of the other view. If `true`, the constraint respects the safe area.
74+
///
75+
/// - Returns: A ``LayoutConstraints`` object containing the top edge constraint.
5276
@discardableResult
5377
public func top(
5478
_ padding: CGFloat = 0,
@@ -73,6 +97,14 @@ extension UIView {
7397
return LayoutConstraints(top: constraint)
7498
}
7599

100+
/// Constrains the view's bottom anchor to the bottom anchor of another view or its superview.
101+
///
102+
/// - Parameters:
103+
/// - padding: The distance between the bottom edges of the two views.
104+
/// - view: The view to which the bottom edge will be constrained. If `nil`, the view's superview is used.
105+
/// - safeArea: A Boolean indicating whether to constrain to the `safeAreaLayoutGuide` of the other view. If `true`, the constraint respects the safe area.
106+
///
107+
/// - Returns: A ``LayoutConstraints`` object containing the bottom edge constraint.
76108
@discardableResult
77109
public func bottom(
78110
_ padding: CGFloat = 0,
@@ -99,6 +131,14 @@ extension UIView {
99131
}
100132

101133
extension UIView {
134+
/// Constrains the view's leading and trailing edges to those of another view or its superview, effectively stretching it horizontally.
135+
///
136+
/// - Parameters:
137+
/// - padding: The distance between the view's edges and the other view's edges.
138+
/// - view: The view to which the horizontal edges will be constrained. If `nil`, the view's superview is used.
139+
/// - safeArea: A Boolean indicating whether to constrain to the `safeAreaLayoutGuide` of the other view. If `true`, the constraints respect the safe area.
140+
///
141+
/// - Returns: A ``LayoutConstraints`` object containing the leading and trailing constraints.
102142
@discardableResult
103143
public func horizontally(
104144
_ padding: CGFloat = 0,
@@ -111,6 +151,14 @@ extension UIView {
111151
}
112152
}
113153

154+
/// Constrains the view's top and bottom edges to those of another view or its superview, effectively stretching it vertically.
155+
///
156+
/// - Parameters:
157+
/// - padding: The distance between the view's edges and the other view's edges.
158+
/// - view: The view to which the vertical edges will be constrained. If `nil`, the view's superview is used.
159+
/// - safeArea: A Boolean indicating whether to constrain to the `safeAreaLayoutGuide` of the other view. If `true`, the constraints respect the safe area.
160+
///
161+
/// - Returns: A ``LayoutConstraints`` object containing the top and bottom constraints.
114162
@discardableResult
115163
public func vertically(
116164
_ padding: CGFloat = 0,
@@ -125,6 +173,14 @@ extension UIView {
125173
}
126174

127175
extension UIView {
176+
/// Constrains all four edges of the view to another view or its superview, optionally with padding and safe area considerations.
177+
///
178+
/// - Parameters:
179+
/// - padding: The distance between the view's edges and the other view's edges.
180+
/// - view: The view to which the edges will be constrained. If `nil`, the view's superview is used.
181+
/// - safeArea: A Boolean indicating whether to constrain to the `safeAreaLayoutGuide` of the other view. If `true`, the constraints respect the safe area.
182+
///
183+
/// - Returns: A ``LayoutConstraints`` object containing top, leading, bottom and trailing constraints.
128184
@discardableResult
129185
public func allEdges(
130186
_ padding: CGFloat = 0,
@@ -139,7 +195,13 @@ extension UIView {
139195
}
140196

141197
extension UIView {
142-
/// Align the view after the passed view.
198+
/// Constrains the view's leading anchor to the trailing anchor of another view, positioning it after the specified view horizontally.
199+
///
200+
/// - Parameters:
201+
/// - view: The view after which this view will be positioned.
202+
/// - padding: The space between the trailing edge of the specified view and the leading edge of this view.
203+
///
204+
/// - Returns: A ``LayoutConstraints`` object containing the leading edge constraint.
143205
@discardableResult
144206
public func after(
145207
_ view: UIView,
@@ -156,7 +218,13 @@ extension UIView {
156218
return LayoutConstraints(leading: constraint)
157219
}
158220

159-
/// Align the view before the passed view.
221+
/// Constrains the view's trailing anchor to the leading anchor of another view, positioning it before the specified view horizontally.
222+
///
223+
/// - Parameters:
224+
/// - view: The view before which this view will be positioned.
225+
/// - padding: The space between the leading edge of the specified view and the trailing edge of this view.
226+
///
227+
/// - Returns: A ``LayoutConstraints`` object containing the trailing edge constraint.
160228
@discardableResult
161229
public func before(
162230
_ view: UIView,
@@ -173,7 +241,13 @@ extension UIView {
173241
return LayoutConstraints(trailing: constraint)
174242
}
175243

176-
/// Align the view below the passed view.
244+
/// Constrains the view's top anchor to the bottom anchor of another view, positioning it below the specified view vertically.
245+
///
246+
/// - Parameters:
247+
/// - view: The view below which this view will be positioned.
248+
/// - padding: The space between the bottom edge of the specified view and the top edge of this view.
249+
///
250+
/// - Returns: A ``LayoutConstraints`` object containing the top edge constraint.
177251
@discardableResult
178252
public func below(
179253
_ view: UIView,
@@ -190,7 +264,13 @@ extension UIView {
190264
return LayoutConstraints(bottom: constraint)
191265
}
192266

193-
/// Align the view above the passed view.
267+
/// Constrains the view's bottom anchor to the top anchor of another view, positioning it above the specified view vertically.
268+
///
269+
/// - Parameters:
270+
/// - view: The view above which this view will be positioned.
271+
/// - padding: The space between the top edge of the specified view and the bottom edge of this view.
272+
///
273+
/// - Returns: A ``LayoutConstraints`` object containing the bottom edge constraint.
194274
@discardableResult
195275
public func above(
196276
_ view: UIView,
@@ -209,6 +289,12 @@ extension UIView {
209289
}
210290

211291
extension UIView {
292+
/// Sets the view's width to a specific value.
293+
///
294+
/// - Parameters:
295+
/// - value: The width value.
296+
///
297+
/// - Returns: A ``LayoutConstraints`` object containing the width constraint.
212298
@discardableResult
213299
public func width(_ constant: CGFloat) -> LayoutConstraints {
214300
self.translatesAutoresizingMaskIntoConstraints = false
@@ -219,6 +305,12 @@ extension UIView {
219305
return LayoutConstraints(width: constraint)
220306
}
221307

308+
/// Sets the view's height to a specific value.
309+
///
310+
/// - Parameters:
311+
/// - value: The height value.
312+
///
313+
/// - Returns: A ``LayoutConstraints`` object containing the height constraint.
222314
@discardableResult
223315
public func height(_ constant: CGFloat) -> LayoutConstraints {
224316
self.translatesAutoresizingMaskIntoConstraints = false
@@ -229,21 +321,41 @@ extension UIView {
229321
return LayoutConstraints(height: constraint)
230322
}
231323

324+
/// Sets the view's size to specific width and height values.
325+
///
326+
/// - Parameters:
327+
/// - width: The width value.
328+
/// - height: The height value.
329+
///
330+
/// - Returns: A ``LayoutConstraints`` object containing the size constraints.
232331
@discardableResult
233-
public func size(_ size: CGSize) -> LayoutConstraints {
332+
public func size(width: CGFloat, height: CGFloat) -> LayoutConstraints {
234333
return LayoutConstraints.merged {
235-
self.width(size.width)
236-
self.height(size.height)
334+
self.width(width)
335+
self.height(height)
237336
}
238337
}
239338

339+
/// Sets the view's size to a specific width and height value.
340+
///
341+
/// - Parameters:
342+
/// - constant: The height and width value.
343+
///
344+
/// - Returns: A ``LayoutConstraints`` object containing the size constraints.
240345
@discardableResult
241346
public func size(_ constant: CGFloat) -> LayoutConstraints {
242-
return self.size(CGSize(width: constant, height: constant))
347+
return self.size(width: constant, height: constant)
243348
}
244349
}
245350

246351
extension UIView {
352+
/// Constrains the view's center Y anchor to the center Y anchor of another view or its superview.
353+
///
354+
/// - Parameters:
355+
/// - offset: The vertical offset between the center Y anchors of the two views.
356+
/// - view: The view to which the center Y will be constrained. If `nil`, the view's superview is used.
357+
///
358+
/// - Returns: A ``LayoutConstraints`` object containing the center Y constraint.
247359
@discardableResult
248360
public func centerVertically(
249361
offset: CGFloat = 0,
@@ -265,6 +377,13 @@ extension UIView {
265377
return LayoutConstraints(vertical: constraint)
266378
}
267379

380+
/// Constrains the view's center X anchor to the center X anchor of another view or its superview.
381+
///
382+
/// - Parameters:
383+
/// - offset: The horizontal offset between the center X anchors of the two views.
384+
/// - view: The view to which the center X will be constrained. If `nil`, the view's superview is used.
385+
///
386+
/// - Returns: A `LayoutConstraints` object containing the center X constraint.
268387
@discardableResult
269388
public func centerHorizontally(
270389
offset: CGFloat = 0,
@@ -286,6 +405,13 @@ extension UIView {
286405
return LayoutConstraints(horizontal: constraint)
287406
}
288407

408+
/// Constrains the view's center anchors to the center anchors of another view or its superview.
409+
///
410+
/// - Parameters:
411+
/// - offset: The offset between the center anchors of the two views.
412+
/// - view: The view to which the center will be constrained. If `nil`, the view's superview is used.
413+
///
414+
/// - Returns: A ``LayoutConstraints`` object containing the center X and center Y constraint.
289415
@discardableResult
290416
public func center(
291417
offset: CGPoint = .zero,

0 commit comments

Comments
 (0)