Skip to content

Commit f98a4a0

Browse files
author
Luc Dion
authored
Merge pull request #80 from mirego/add_hCenter_vCenter_to_edges
Add methods to pin hCenter and vCenter to any other view's edges (inc…
2 parents b7f41da + eb3cfd8 commit f98a4a0

File tree

7 files changed

+99
-23
lines changed

7 files changed

+99
-23
lines changed

Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PODS:
22
- Nimble (7.0.1)
33
- Quick (1.1.0)
4-
- Reveal-SDK (10)
4+
- Reveal-SDK (11)
55
- SwiftLint (0.22.0)
66

77
DEPENDENCIES:
@@ -13,7 +13,7 @@ DEPENDENCIES:
1313
SPEC CHECKSUMS:
1414
Nimble: 657d000e11df8aebe27cdaf9d244de7f30ed87f7
1515
Quick: dafc587e21eed9f4cab3249b9f9015b0b7a7f71d
16-
Reveal-SDK: 7869ddf1f902cabbb07a1f0dd06bd25861a126f7
16+
Reveal-SDK: 7fa13d04bb55db61ff2342a990cf731d5159361d
1717
SwiftLint: 1134786caedd2caab0560d2f36b76414a5a56808
1818

1919
PODFILE CHECKSUM: 3b67fc05230707b4ff0a4b82edf3192f17967214

Sources/PinLayout.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,25 @@ public protocol AnchorList {
9898
UIView's Edges
9999
======================
100100
top
101-
+-------------------+
102-
| |
103-
| |
104-
| |
105-
left | | right
106-
| |
107-
| |
108-
| |
109-
| |
110-
+-------------------+
101+
+-----------------+
102+
| |
103+
| |
104+
| hCenter |
105+
left | + | right
106+
| vCenter |
107+
| |
108+
| |
109+
+-----------------+
111110
bottom
112111
*/
113112

114113
/// UIViews's list of edges
115114
public protocol EdgeList {
116115
var top: VerticalEdge { get }
117-
var left: HorizontalEdge { get }
116+
var vCenter: VerticalEdge { get }
118117
var bottom: VerticalEdge { get }
118+
var left: HorizontalEdge { get }
119+
var hCenter: HorizontalEdge { get }
119120
var right: HorizontalEdge { get }
120121

121122
// RTL support
@@ -164,8 +165,10 @@ public protocol PinLayout {
164165
// MARK: Layout using edges
165166
//
166167
@discardableResult func top(to edge: VerticalEdge) -> PinLayout
167-
@discardableResult func left(to edge: HorizontalEdge) -> PinLayout
168+
@discardableResult func vCenter(to edge: VerticalEdge) -> PinLayout
168169
@discardableResult func bottom(to edge: VerticalEdge) -> PinLayout
170+
@discardableResult func left(to edge: HorizontalEdge) -> PinLayout
171+
@discardableResult func hCenter(to edge: HorizontalEdge) -> PinLayout
169172
@discardableResult func right(to edge: HorizontalEdge) -> PinLayout
170173
// RTL support
171174
@discardableResult func start(to edge: HorizontalEdge) -> PinLayout

Sources/PinLayoutImpl.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,24 @@ class PinLayoutImpl: PinLayout {
239239
return self
240240
}
241241

242+
@discardableResult
243+
func vCenter(to edge: VerticalEdge) -> PinLayout {
244+
func context() -> String { return relativeEdgeContext(method: "vCenter", edge: edge) }
245+
if let coordinate = computeCoordinate(forEdge: edge, context) {
246+
setVerticalCenter(coordinate, context)
247+
}
248+
return self
249+
}
250+
251+
@discardableResult
252+
func bottom(to edge: VerticalEdge) -> PinLayout {
253+
func context() -> String { return relativeEdgeContext(method: "bottom", edge: edge) }
254+
if let coordinate = computeCoordinate(forEdge: edge, context) {
255+
setBottom(coordinate, context)
256+
}
257+
return self
258+
}
259+
242260
@discardableResult
243261
func left(to edge: HorizontalEdge) -> PinLayout {
244262
func context() -> String { return relativeEdgeContext(method: "left", edge: edge) }
@@ -249,10 +267,10 @@ class PinLayoutImpl: PinLayout {
249267
}
250268

251269
@discardableResult
252-
func bottom(to edge: VerticalEdge) -> PinLayout {
253-
func context() -> String { return relativeEdgeContext(method: "bottom", edge: edge) }
270+
func hCenter(to edge: HorizontalEdge) -> PinLayout {
271+
func context() -> String { return relativeEdgeContext(method: "hCenter", edge: edge) }
254272
if let coordinate = computeCoordinate(forEdge: edge, context) {
255-
setBottom(coordinate, context)
273+
setHorizontalCenter(coordinate, context)
256274
}
257275
return self
258276
}

Sources/TypesImpl.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ class EdgeListImpl: EdgeList {
4242
}
4343

4444
var top: VerticalEdge { return VerticalEdgeImpl(view: view, type: .top) }
45-
var left: HorizontalEdge { return HorizontalEdgeImpl(view: view, type: .left) }
45+
var vCenter: VerticalEdge { return VerticalEdgeImpl(view: view, type: .vCenter) }
4646
var bottom: VerticalEdge { return VerticalEdgeImpl(view: view, type: .bottom) }
47+
48+
var left: HorizontalEdge { return HorizontalEdgeImpl(view: view, type: .left) }
49+
var hCenter: HorizontalEdge { return HorizontalEdgeImpl(view: view, type: .hCenter) }
4750
var right: HorizontalEdge { return HorizontalEdgeImpl(view: view, type: .right) }
4851

4952
// RTL support
@@ -54,6 +57,7 @@ class EdgeListImpl: EdgeList {
5457
class HorizontalEdgeImpl: HorizontalEdge {
5558
enum EdgeType: String {
5659
case left
60+
case hCenter
5761
case right
5862
}
5963

@@ -62,8 +66,9 @@ class HorizontalEdgeImpl: HorizontalEdge {
6266

6367
var x: CGFloat {
6468
switch type {
65-
case .left: return view.frame.origin.x
66-
case .right: return view.frame.maxX
69+
case .left: return view.frame.origin.x
70+
case .hCenter: return view.frame.midX
71+
case .right: return view.frame.maxX
6772
}
6873
}
6974

@@ -76,6 +81,7 @@ class HorizontalEdgeImpl: HorizontalEdge {
7681
class VerticalEdgeImpl: VerticalEdge {
7782
enum EdgeType: String {
7883
case top
84+
case vCenter
7985
case bottom
8086
}
8187

@@ -84,8 +90,9 @@ class VerticalEdgeImpl: VerticalEdge {
8490

8591
var y: CGFloat {
8692
switch type {
87-
case .top: return view.frame.origin.y
88-
case .bottom: return view.frame.maxY
93+
case .top: return view.frame.origin.y
94+
case .vCenter: return view.frame.midY
95+
case .bottom: return view.frame.maxY
8996
}
9097
}
9198

Tests/PinEdgesSpec.swift

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class PinEdgesSpec: QuickSpec {
3535

3636
var rootView: BasicView!
3737
var aView: BasicView!
38-
38+
var bView: BasicView!
39+
var bViewChild: BasicView!
40+
3941
/*
4042
root
4143
|
@@ -56,6 +58,14 @@ class PinEdgesSpec: QuickSpec {
5658
aView = BasicView(text: "View A", color: UIColor.red.withAlphaComponent(0.5))
5759
aView.frame = CGRect(x: 140, y: 100, width: 200, height: 100)
5860
rootView.addSubview(aView)
61+
62+
bView = BasicView(text: "View B", color: UIColor.blue.withAlphaComponent(0.5))
63+
bView.frame = CGRect(x: 160, y: 120, width: 110, height: 80)
64+
rootView.addSubview(bView)
65+
66+
bViewChild = BasicView(text: "View B Child", color: UIColor.blue.withAlphaComponent(0.7))
67+
bViewChild.frame = CGRect(x: 40, y: 10, width: 60, height: 20)
68+
bView.addSubview(bViewChild)
5969
}
6070

6171
//
@@ -339,6 +349,7 @@ class PinEdgesSpec: QuickSpec {
339349
expect(_pinlayoutUnitTestLastWarning).to(contain(["hCenter", "won't be applied", "left"]))
340350
}
341351

352+
// hCenter(%)
342353
it("should warns that the view is not added to any view") {
343354
let unAttachedView = UIView(frame: CGRect(x: 10, y: 10, width: 10, height: 10))
344355
unAttachedView.pin.hCenter(20%)
@@ -355,6 +366,22 @@ class PinEdgesSpec: QuickSpec {
355366
aView.pin.hCenter(-20%)
356367
expect(aView.frame).to(equal(CGRect(x: -180, y: 100.0, width: 200.0, height: 100.0)))
357368
}
369+
370+
// hCenter(to: ...)
371+
it("should adjust the bView") {
372+
bView.pin.hCenter(to: aView.edge.left)
373+
expect(bView.frame).to(equal(CGRect(x: 85.0, y: 120.0, width: 110.0, height: 80.0)))
374+
}
375+
376+
it("should adjust the bView") {
377+
bView.pin.hCenter(to: aView.edge.hCenter)
378+
expect(bView.frame).to(equal(CGRect(x: 185.0, y: 120.0, width: 110.0, height: 80.0)))
379+
}
380+
381+
it("should adjust the bView") {
382+
bView.pin.hCenter(to: aView.edge.right)
383+
expect(bView.frame).to(equal(CGRect(x: 285.0, y: 120.0, width: 110.0, height: 80.0)))
384+
}
358385
}
359386

360387
//
@@ -413,6 +440,27 @@ class PinEdgesSpec: QuickSpec {
413440
expect(aView.frame).to(equal(CGRect(x: 140, y: 0.0, width: 200.0, height: 100.0)))
414441
expect(_pinlayoutUnitTestLastWarning).to(contain(["vCenter", "won't be applied", "top"]))
415442
}
443+
444+
// vCenter(to: ...)
445+
it("should adjust the bView") {
446+
bView.pin.vCenter(to: aView.edge.top)
447+
expect(bView.frame).to(equal(CGRect(x: 160.0, y: 60.0, width: 110.0, height: 80.0)))
448+
}
449+
450+
it("should adjust the bView") {
451+
bView.pin.vCenter(to: aView.edge.vCenter)
452+
expect(bView.frame).to(equal(CGRect(x: 160.0, y: 110.0, width: 110.0, height: 80.0)))
453+
}
454+
455+
it("should adjust the bView") {
456+
bView.pin.vCenter(to: aView.edge.bottom)
457+
expect(bView.frame).to(equal(CGRect(x: 160.0, y: 160.0, width: 110.0, height: 80.0)))
458+
}
459+
460+
it("should adjust the bView") {
461+
bViewChild.pin.vCenter(to: aView.edge.vCenter)
462+
expect(bViewChild.frame).to(equal(CGRect(x: 40.0, y: 20.0, width: 60.0, height: 20.0)))
463+
}
416464
}
417465
}
418466
}
43.2 KB
Binary file not shown.

docs/pinlayout-edges.png

4.61 KB
Loading

0 commit comments

Comments
 (0)