Skip to content

Commit d18005b

Browse files
create a library with helper methods for autolayout
0 parents  commit d18005b

File tree

6 files changed

+446
-0
lines changed

6 files changed

+446
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Package.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version:5.9
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "AutoLayout",
7+
platforms: [
8+
.iOS(.v13)
9+
],
10+
products: [
11+
.library(
12+
name: "AutoLayout",
13+
targets: ["AutoLayout"]
14+
)
15+
],
16+
targets: [
17+
.target(
18+
name: "AutoLayout"
19+
)
20+
]
21+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
extension LayoutConstraints {
2+
public static func merged(@LayoutConstraintsBuilder _ constraints: () -> LayoutConstraints) -> LayoutConstraints {
3+
return constraints()
4+
}
5+
}
6+
7+
extension LayoutConstraints {
8+
public mutating func merge(with constraint: LayoutConstraints) {
9+
self = LayoutConstraints.merged {
10+
self
11+
constraint
12+
}
13+
}
14+
15+
public mutating func merge(@LayoutConstraintsBuilder with constraints: () -> LayoutConstraints) {
16+
self = LayoutConstraints.merged {
17+
self
18+
constraints()
19+
}
20+
}
21+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import UIKit
2+
3+
/// Contains constraints of the view.
4+
public struct LayoutConstraints {
5+
/// Top constraint.
6+
public var top: NSLayoutConstraint?
7+
/// Leading constraint.
8+
public var leading: NSLayoutConstraint?
9+
/// Bottom constraint.
10+
public var bottom: NSLayoutConstraint?
11+
/// Trailing constraint.
12+
public var trailing: NSLayoutConstraint?
13+
/// Vertical constraint.
14+
public var vertical: NSLayoutConstraint?
15+
/// Horizontal constraint.
16+
public var horizontal: NSLayoutConstraint?
17+
/// Width constraint.
18+
public var width: NSLayoutConstraint?
19+
/// Height constraint.
20+
public var height: NSLayoutConstraint?
21+
22+
/// Array of all constraints.
23+
public var allConstraints: [NSLayoutConstraint?] {
24+
return [
25+
self.top,
26+
self.leading,
27+
self.bottom,
28+
self.trailing,
29+
self.vertical,
30+
self.horizontal,
31+
self.width,
32+
self.height
33+
]
34+
}
35+
36+
/// Memberwise initializer.
37+
public init(
38+
top: NSLayoutConstraint? = nil,
39+
leading: NSLayoutConstraint? = nil,
40+
bottom: NSLayoutConstraint? = nil,
41+
trailing: NSLayoutConstraint? = nil,
42+
vertical: NSLayoutConstraint? = nil,
43+
horizontal: NSLayoutConstraint? = nil,
44+
width: NSLayoutConstraint? = nil,
45+
height: NSLayoutConstraint? = nil
46+
) {
47+
self.top = top
48+
self.leading = leading
49+
self.bottom = bottom
50+
self.trailing = trailing
51+
self.vertical = vertical
52+
self.horizontal = horizontal
53+
self.width = width
54+
self.height = height
55+
}
56+
57+
/// Activates existing constraints.
58+
public func activateAll() {
59+
self.allConstraints.forEach { $0?.isActive = true }
60+
}
61+
62+
/// Deactivates existing constraints.
63+
public func deactivateAll() {
64+
self.allConstraints.forEach { $0?.isActive = false }
65+
}
66+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@resultBuilder
2+
public struct LayoutConstraintsBuilder {
3+
public static func buildBlock(_ constraints: LayoutConstraints...) -> LayoutConstraints {
4+
constraints.reduce(into: LayoutConstraints()) { partialResult, constraints in
5+
if let top = constraints.top {
6+
partialResult.top = top
7+
}
8+
if let leading = constraints.leading {
9+
partialResult.leading = leading
10+
}
11+
if let bottom = constraints.bottom {
12+
partialResult.bottom = bottom
13+
}
14+
if let trailing = constraints.trailing {
15+
partialResult.trailing = trailing
16+
}
17+
if let vertical = constraints.vertical {
18+
partialResult.vertical = vertical
19+
}
20+
if let horizontal = constraints.horizontal {
21+
partialResult.horizontal = horizontal
22+
}
23+
if let width = constraints.width {
24+
partialResult.width = width
25+
}
26+
if let height = constraints.height {
27+
partialResult.height = height
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)