Skip to content

Commit e50c8f6

Browse files
committed
feat: improve code again
1 parent d183080 commit e50c8f6

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

Sources/Swutil/Extensions/UIStackView+AddArrangedSubviews.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import UIKit
99

1010
public extension UIStackView {
1111
func addArrangedSubviews(_ arrangedSubviews: [UIView]) {
12-
arrangedSubviews.forEach {
13-
$0.translatesAutoresizingMaskIntoConstraints = false
14-
addArrangedSubview($0)
15-
}
12+
arrangedSubviews.forEach { addArrangedSubview($0) }
1613
}
1714
}

Sources/Swutil/Extensions/UIView+Constraints.swift

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import UIKit
99

1010
public extension UIView {
1111

12+
/// `Enum` defining the different possible distances for constraints.
13+
///
14+
/// equalTo
15+
/// lessThanOrEqualTo
16+
/// greaterThanOrEqualTo
1217
enum ConstraintDistance {
1318
case equalTo
1419
case lessThanOrEqualTo
@@ -33,6 +38,24 @@ public extension UIView {
3338
return self
3439
}
3540

41+
/// Constraint the current view topAnchor with the specified view topAnchor.
42+
/// - Parameters:
43+
/// - view: The view to be used as reference.
44+
/// - constant: A custom constant to be used in the constraints.
45+
/// - distance: Enum controlling if the constraint should be equalTo, lessThanOrEqualTo or greaterThanOrEqualTo the anchor.
46+
@discardableResult
47+
func top(to view: UIView, constant: CGFloat = .zero, distance: ConstraintDistance = .equalTo) -> Self {
48+
switch distance {
49+
case .equalTo:
50+
topAnchor.constraint(equalTo: view.topAnchor, constant: constant).isActive = true
51+
case .greaterThanOrEqualTo:
52+
topAnchor.constraint(greaterThanOrEqualTo: view.topAnchor, constant: constant).isActive = true
53+
case .lessThanOrEqualTo:
54+
topAnchor.constraint(lessThanOrEqualTo: view.topAnchor, constant: constant).isActive = true
55+
}
56+
return self
57+
}
58+
3659
/// Constraint the current view leadingAnchor with the anchor received by parameter.
3760
/// - Parameters:
3861
/// - anchor: The layout anchor to be used as reference.
@@ -51,6 +74,24 @@ public extension UIView {
5174
return self
5275
}
5376

77+
/// Constraint the current view leadingAnchor with the specified view leadingAnchor.
78+
/// - Parameters:
79+
/// - view: The view to be used as reference.
80+
/// - constant: A custom constant to be used in the constraints.
81+
/// - distance: Enum controlling if the constraint should be equalTo, lessThanOrEqualTo or greaterThanOrEqualTo the anchor.
82+
@discardableResult
83+
func leading(to view: UIView, constant: CGFloat = .zero, distance: ConstraintDistance = .equalTo) -> Self {
84+
switch distance {
85+
case .equalTo:
86+
leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: constant).isActive = true
87+
case .greaterThanOrEqualTo:
88+
leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: constant).isActive = true
89+
case .lessThanOrEqualTo:
90+
leadingAnchor.constraint(lessThanOrEqualTo: view.leadingAnchor, constant: constant).isActive = true
91+
}
92+
return self
93+
}
94+
5495
/// Constraint the current view trailingAnchor with the anchor received by parameter.
5596
/// - Parameters:
5697
/// - anchor: The layout anchor to be used as reference.
@@ -69,6 +110,24 @@ public extension UIView {
69110
return self
70111
}
71112

113+
/// Constraint the current view trailingAnchor with the specified view trailingAnchor.
114+
/// - Parameters:
115+
/// - view: The view to be used as reference.
116+
/// - constant: A custom constant to be used in the constraints.
117+
/// - distance: Enum controlling if the constraint should be equalTo, lessThanOrEqualTo or greaterThanOrEqualTo the anchor.
118+
@discardableResult
119+
func trailing(to view: UIView, constant: CGFloat = .zero, distance: ConstraintDistance = .equalTo) -> Self {
120+
switch distance {
121+
case .equalTo:
122+
trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: constant).isActive = true
123+
case .greaterThanOrEqualTo:
124+
trailingAnchor.constraint(greaterThanOrEqualTo: view.trailingAnchor, constant: constant).isActive = true
125+
case .lessThanOrEqualTo:
126+
trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: constant).isActive = true
127+
}
128+
return self
129+
}
130+
72131
/// Constraint the current view bottomAnchor with the anchor received by parameter.
73132
/// - Parameters:
74133
/// - anchor: The layout anchor to be used as reference.
@@ -87,6 +146,24 @@ public extension UIView {
87146
return self
88147
}
89148

149+
/// Constraint the current view bottomAnchor with the specified view bottomAnchor.
150+
/// - Parameters:
151+
/// - view: The view to be used as reference.
152+
/// - constant: A custom constant to be used in the constraints.
153+
/// - distance: Enum controlling if the constraint should be equalTo, lessThanOrEqualTo or greaterThanOrEqualTo the anchor.
154+
@discardableResult
155+
func bottom(to view: UIView, constant: CGFloat = .zero, distance: ConstraintDistance = .equalTo) -> Self {
156+
switch distance {
157+
case .equalTo:
158+
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: constant).isActive = true
159+
case .greaterThanOrEqualTo:
160+
bottomAnchor.constraint(greaterThanOrEqualTo: view.bottomAnchor, constant: constant).isActive = true
161+
case .lessThanOrEqualTo:
162+
bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: constant).isActive = true
163+
}
164+
return self
165+
}
166+
90167
/// Constraint the current view centerY and centerX anchors with the anchor received by parameter.
91168
/// - Parameters:
92169
/// - view: The view to be used as reference.

0 commit comments

Comments
 (0)