forked from robb/Cartography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstrain.swift
142 lines (122 loc) · 5.51 KB
/
Constrain.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
//
// Constrain.swift
// Cartography
//
// Created by Robert Böhnke on 30/09/14.
// Copyright (c) 2014 Robert Böhnke. All rights reserved.
//
import Foundation
/// Updates the constraints of a single view.
///
/// - parameter view: The view to layout.
/// - parameter replace: The `ConstraintGroup` whose constraints should be
/// replaced.
/// - parameter block: A block that declares the layout for `view`.
///
public func constrain(view: View, replace group: ConstraintGroup = ConstraintGroup(), @noescape block: LayoutProxy -> ()) -> ConstraintGroup {
let context = Context()
block(LayoutProxy(context, view))
group.replaceConstraints(context.constraints)
return group
}
/// Updates the constraints of two views.
///
/// - parameter view1: A view to layout.
/// - parameter view2: A view to layout.
/// - parameter replace: The `ConstraintGroup` whose constraints should be
/// replaced.
/// - parameter block: A block that declares the layout for the views.
///
public func constrain(view1: View, _ view2: View, replace group: ConstraintGroup = ConstraintGroup(), @noescape block: (LayoutProxy, LayoutProxy) -> ()) -> ConstraintGroup {
let context = Context()
block(LayoutProxy(context, view1), LayoutProxy(context, view2))
group.replaceConstraints(context.constraints)
return group
}
/// Updates the constraints of three views.
///
/// - parameter view1: A view to layout.
/// - parameter view2: A view to layout.
/// - parameter view3: A view to layout.
/// - parameter replace: The `ConstraintGroup` whose constraints should be
/// replaced.
/// - parameter block: A block that declares the layout for the views.
///
public func constrain(view1: View, _ view2: View, _ view3: View, replace group: ConstraintGroup = ConstraintGroup(), @noescape block: (LayoutProxy, LayoutProxy, LayoutProxy) -> ()) -> ConstraintGroup {
let context = Context()
block(LayoutProxy(context, view1), LayoutProxy(context, view2), LayoutProxy(context, view3))
group.replaceConstraints(context.constraints)
return group
}
/// Updates the constraints of four views.
///
/// - parameter view1: A view to layout.
/// - parameter view2: A view to layout.
/// - parameter view3: A view to layout.
/// - parameter view4: A view to layout.
/// - parameter replace: The `ConstraintGroup` whose constraints should be
/// replaced.
/// - parameter block: A block that declares the layout for the views.
///
public func constrain(view1: View, _ view2: View, _ view3: View, _ view4: View, replace group: ConstraintGroup = ConstraintGroup(), @noescape block: (LayoutProxy, LayoutProxy, LayoutProxy, LayoutProxy) -> ()) -> ConstraintGroup {
let context = Context()
block(LayoutProxy(context, view1), LayoutProxy(context, view2), LayoutProxy(context, view3), LayoutProxy(context, view4))
group.replaceConstraints(context.constraints)
return group
}
/// Updates the constraints of five views.
///
/// - parameter view1: A view to layout.
/// - parameter view2: A view to layout.
/// - parameter view3: A view to layout.
/// - parameter view4: A view to layout.
/// - parameter view5: A view to layout.
/// - parameter replace: The `ConstraintGroup` whose constraints should be
/// replaced.
/// - parameter block: A block that declares the layout for the views.
///
public func constrain(view1: View, _ view2: View, _ view3: View, _ view4: View, _ view5: View, replace group: ConstraintGroup = ConstraintGroup(), @noescape block: (LayoutProxy, LayoutProxy, LayoutProxy, LayoutProxy, LayoutProxy) -> ()) -> ConstraintGroup {
let context = Context()
block(LayoutProxy(context, view1), LayoutProxy(context, view2), LayoutProxy(context, view3), LayoutProxy(context, view4), LayoutProxy(context, view5))
group.replaceConstraints(context.constraints)
return group
}
/// Updates the constraints of an array of views.
///
/// - parameter views: The views to layout.
/// - parameter replace: The `ConstraintGroup` whose constraints should be
/// replaced.
/// - parameter block: A block that declares the layout for `views`.
///
public func constrain(views: [View], replace group: ConstraintGroup = ConstraintGroup(), @noescape block: ([LayoutProxy]) -> ()) -> ConstraintGroup {
let context = Context()
block(views.map({ LayoutProxy(context, $0) }))
group.replaceConstraints(context.constraints)
return group
}
/// Updates the constraints of a dictionary of views.
///
/// - parameter views: The views to layout.
/// - parameter replace: The `ConstraintGroup` whose constraints should be
/// replaced.
/// - parameter block: A block that declares the layout for `views`.
///
public func constrain<T: Hashable>(views: [T: View], replace group: ConstraintGroup = ConstraintGroup(), @noescape block: ([T : LayoutProxy] -> ())) -> ConstraintGroup {
let context = Context()
// this conflict with ObjectMapper Dictionary extension
// let proxies = views.map { ($0, LayoutProxy(context, $1)) }
var proxies = [T : LayoutProxy]()
for view in views {
proxies.updateValue(LayoutProxy(context, view.1), forKey: view.0)
}
block(proxies)
group.replaceConstraints(context.constraints)
return group
}
/// Removes all constraints for a group.
///
/// - parameter clear: The `ConstraintGroup` whose constraints should be removed.
///
public func constrain(clear group: ConstraintGroup) {
group.replaceConstraints([])
}