-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutConstraint.swift
More file actions
255 lines (202 loc) · 8.84 KB
/
LayoutConstraint.swift
File metadata and controls
255 lines (202 loc) · 8.84 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// Autolayout.swift
// Matrix
//
// Created by Mark Onyschuk on 2014-06-19.
// Copyright (c) 2014 Mark Onyschuk. All rights reserved.
//
#if os(iOS)
import UIKit
typealias LayoutView = UIView
typealias LayoutPriority = UILayoutPriority
#elseif os(OSX)
import Cocoa
typealias LayoutView = NSView
typealias LayoutPriority = NSLayoutPriority
#endif
struct LayoutAttribute {
var view: LayoutView?
var attribute: NSLayoutAttribute
}
struct LayoutRelation {
init(from: LayoutAttribute, relationship: NSLayoutRelation, to: LayoutAttribute) {
self.to = to
self.from = from
self.relationship = relationship
}
var from, to: LayoutAttribute
var relationship: NSLayoutRelation
// optional
var constant = 0.0
var multiplier = 1.0
var priority = LayoutPriority(1000)
var layoutConstraint: NSLayoutConstraint {
let layoutConstraint = NSLayoutConstraint(
item: self.from.view,
attribute: self.from.attribute,
relatedBy: self.relationship,
toItem: self.to.view,
attribute: self.to.attribute,
multiplier: CGFloat(self.multiplier),
constant: CGFloat(self.constant))
layoutConstraint.priority = self.priority
return layoutConstraint
}
}
// Attribute-Attribute relations
func ==(lhs: LayoutAttribute, rhs: LayoutAttribute) -> LayoutRelation {
return LayoutRelation(from: lhs, relationship: .Equal, to: rhs)
}
func <=(lhs: LayoutAttribute, rhs: LayoutAttribute) -> LayoutRelation {
return LayoutRelation(from: lhs, relationship: .LessThanOrEqual, to: rhs)
}
func >=(lhs: LayoutAttribute, rhs: LayoutAttribute) -> LayoutRelation {
return LayoutRelation(from: lhs, relationship: .GreaterThanOrEqual, to: rhs)
}
// Attribute-Constant relations
func ==(lhs: LayoutAttribute, rhs: Double) -> LayoutRelation {
var to = LayoutAttribute(view: nil, attribute: .NotAnAttribute)
var relation = LayoutRelation(from: lhs, relationship: .Equal, to: to)
relation.constant = rhs
return relation
}
func <=(lhs: LayoutAttribute, rhs: Double) -> LayoutRelation {
var to = LayoutAttribute(view: nil, attribute: .NotAnAttribute)
var relation = LayoutRelation(from: lhs, relationship: .LessThanOrEqual, to: to)
relation.constant = rhs
return relation
}
func >=(lhs: LayoutAttribute, rhs: Double) -> LayoutRelation {
var to = LayoutAttribute(view: nil, attribute: .NotAnAttribute)
var relation = LayoutRelation(from: lhs, relationship: .GreaterThanOrEqual, to: to)
relation.constant = rhs
return relation
}
func ==(lhs: LayoutAttribute, rhs: Int) -> LayoutRelation {
var to = LayoutAttribute(view: nil, attribute: .NotAnAttribute)
var relation = LayoutRelation(from: lhs, relationship: .Equal, to: to)
relation.constant = Double(rhs)
return relation
}
func <=(lhs: LayoutAttribute, rhs: Int) -> LayoutRelation {
var to = LayoutAttribute(view: nil, attribute: .NotAnAttribute)
var relation = LayoutRelation(from: lhs, relationship: .LessThanOrEqual, to: to)
relation.constant = Double(rhs)
return relation
}
func >=(lhs: LayoutAttribute, rhs: Int) -> LayoutRelation {
var to = LayoutAttribute(view: nil, attribute: .NotAnAttribute)
var relation = LayoutRelation(from: lhs, relationship: .GreaterThanOrEqual, to: to)
relation.constant = Double(rhs)
return relation
}
enum LayoutOption {
case constant(Double), multiplier(Double), priority(LayoutPriority)
}
func &&(var lhs: LayoutRelation, rhs: LayoutOption) -> LayoutRelation {
switch rhs {
case let .priority(value): lhs.priority = value
case let .constant(value): lhs.constant = value
case let .multiplier(value): lhs.multiplier = value
}
return lhs
}
// An extension to NSView and UIView allowing constraints to be specified in a compact,
// descriptive format:
//
// let v1 = NSView(frame: NSRect.zeroRect)
// let v2 = NSView(frame: NSRect.zeroRect)
//
// v1.addSubview(v2)
//
// v1.addConstraints([
// v1.layoutLeft == v2.layoutLeft,
// v1.layoutRight == v2.layoutRight && .priority(750),
// v1.layoutCenterY == v2.layoutCenterY && .constant(-10.0)
// ])
//
// v2.addConstraint(v2.layoutHeight == 14)
//
extension LayoutView {
var layoutTop: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Top)}
var layoutLeft: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Left)}
var layoutRight: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Right)}
var layoutBottom: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Bottom)}
var layoutLeading: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Leading)}
var layoutTrailing: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Trailing)}
var layoutWidth: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Width)}
var layoutHeight: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Height)}
var layoutCenterX: LayoutAttribute {return LayoutAttribute(view: self, attribute: .CenterX)}
var layoutCenterY: LayoutAttribute {return LayoutAttribute(view: self, attribute: .CenterY)}
var layoutBaseline: LayoutAttribute {return LayoutAttribute(view: self, attribute: .Baseline)}
// Adding Constraint(s)
func addConstraint(relation: LayoutRelation) -> NSLayoutConstraint {
let layoutConstraint = relation.layoutConstraint
self.addConstraint(layoutConstraint)
return layoutConstraint
}
func addConstraints(constraints: [LayoutRelation]) -> [NSLayoutConstraint] {
var result: [NSLayoutConstraint] = []
for relation in constraints {
result.append(addConstraint(relation))
}
return result
}
}
// Layouts
protocol Layout {
typealias ViewType
var views: [ViewType] {get}
var constraints: [NSLayoutConstraint] {get}
}
// Common Layouts:
// ListLayout represents a vertical or horizontal list of views within a parent view - eg.
// horizontalButtonLayout = ListLayout(views: buttons, container: buttonContainer, orientation: .Horizontal, trailingRelationFn: ==)
typealias LayoutRelationFn = (LayoutAttribute, LayoutAttribute) -> LayoutRelation
class ListLayout<T: LayoutView>: Layout {
var container: LayoutView
var orientation: NSLayoutConstraintOrientation
var trailingRelationFn: LayoutRelationFn!
init(views: [T], container: LayoutView, orientation: NSLayoutConstraintOrientation, trailingRelationFn: LayoutRelationFn! = nil) {
self.views = views
self.container = container
self.orientation = orientation
self.trailingRelationFn = trailingRelationFn
// layout builder
var prev: T! = nil
var axisRelations = [LayoutRelation]()
var offAxisRelations = [LayoutRelation]()
for v in self.views {
if v.superview != self.container {
self.container.addSubview(v)
}
switch self.orientation {
case .Vertical:
axisRelations += [(v.layoutTop == (prev ? prev.layoutBottom : v.superview!.layoutTop)) && .priority(750)]
offAxisRelations += [v.layoutLeft == v.superview!.layoutLeft && .priority(750), v.layoutRight == v.superview!.layoutRight && .priority(750)]
case .Horizontal:
axisRelations += [(v.layoutLeading == (prev ? prev.layoutTrailing : v.superview!.layoutLeading)) && .priority(750)]
offAxisRelations += [v.layoutTop == v.superview!.layoutTop && .priority(750), v.layoutBottom == v.superview!.layoutBottom && .priority(750)]
}
prev = v
}
if prev {
if self.trailingRelationFn {
switch self.orientation {
case .Vertical:
axisRelations += [self.trailingRelationFn(prev.layoutBottom, prev.superview!.layoutBottom) && .priority(750)]
case .Horizontal:
axisRelations += [self.trailingRelationFn(prev.layoutTrailing, prev.superview!.layoutTrailing) && .priority(750)]
}
}
}
self.axisConstraints = axisRelations.map {$0.layoutConstraint}
self.offAxisConstraints = offAxisRelations.map {$0.layoutConstraint}
}
// Layout Protocol
var views: [T]
var constraints: [NSLayoutConstraint] { return self.axisConstraints + self.offAxisConstraints }
// Layout Protocol Support
var axisConstraints: [NSLayoutConstraint] // constraints along self.orientation
var offAxisConstraints: [NSLayoutConstraint] // constraints perpendicular to self.orientation
}