Skip to content

Commit cc28684

Browse files
committed
Add support for UIViewController layout guides
1 parent 3e9d219 commit cc28684

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## SwiftAutoLayout
22

3-
SwiftAutoLayout is a tiny DSL for Autolayout (~90 LOC), intended to provide a more declarative way to express layout constraints. Here's a quick example:
3+
SwiftAutoLayout is a tiny DSL for Autolayout intended to provide a more declarative way to express layout constraints. Here's a quick example:
44

55
```swift
66
// this:
@@ -23,6 +23,8 @@ SwiftAutoLayout allows you to more effectively communicate the intent of a const
2323

2424
Layout attributes are defined as properties added in extensions of `UIView` and `UILayoutGuide` on iOS and `NSView` and `NSLayoutGuide` on OS X. For example, `UIView.width` and `UIView.height` represent `NSLayoutAttribute.Width` and `NSLayoutAttribute.Height`, respectively.
2525

26+
Layout guides (conforming to `UILayoutSupport`) in `UIViewController` are also supported using the `topLayoutGuideTop`, `topLayoutGuideBottom`, `bottomLayoutGuideTop`, and `bottomLayoutGuideBottom` properties.
27+
2628
### Relations
2729

2830
Relations are expressed using the overloaded operators `==` (`NSLayoutRelation.Equal`), `>=` (`NSLayoutRelation.GreaterThanOrEqual`), and `<=` (`NSLayoutRelation.LessThanOrEqual`).

SwiftAutoLayout/SwiftAutoLayout.swift

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,43 @@ public func <=(lhs: LayoutItem<Dimension>, rhs: CGFloat) -> NSLayoutConstraint {
9090
return lhs.constrain(rhs, relation: .LessThanOrEqual)
9191
}
9292

93+
private func layoutItem<C>(item: AnyObject, _ attribute: NSLayoutAttribute) -> LayoutItem<C> {
94+
return LayoutItem(item: item, attribute: attribute, multiplier: 1.0, constant: 0.0)
95+
}
96+
9397
public extension LayoutRegion {
94-
private func layoutItem<C>(attribute: NSLayoutAttribute) -> LayoutItem<C> {
95-
return LayoutItem(item: self, attribute: attribute, multiplier: 1.0, constant: 0.0)
98+
public var left: LayoutItem<XAxis> { return layoutItem(self, .Left) }
99+
public var right: LayoutItem<XAxis> { return layoutItem(self, .Right) }
100+
public var top: LayoutItem<YAxis> { return layoutItem(self, .Top) }
101+
public var bottom: LayoutItem<YAxis> { return layoutItem(self, .Bottom) }
102+
public var leading: LayoutItem<XAxis> { return layoutItem(self, .Leading) }
103+
public var trailing: LayoutItem<XAxis> { return layoutItem(self, .Trailing) }
104+
public var width: LayoutItem<Dimension> { return layoutItem(self, .Width) }
105+
public var height: LayoutItem<Dimension> { return layoutItem(self, .Height) }
106+
public var centerX: LayoutItem<XAxis> { return layoutItem(self, .CenterX) }
107+
public var centerY: LayoutItem<YAxis> { return layoutItem(self, .CenterY) }
108+
public var baseline: LayoutItem<YAxis> { return layoutItem(self, .Baseline) }
109+
}
110+
111+
#if os(iOS)
112+
public extension UIViewController {
113+
public var topLayoutGuideTop: LayoutItem<YAxis> {
114+
return layoutItem(topLayoutGuide, .Top)
115+
}
116+
117+
public var topLayoutGuideBottom: LayoutItem<YAxis> {
118+
return layoutItem(topLayoutGuide, .Bottom)
119+
}
120+
121+
public var bottomLayoutGuideTop: LayoutItem<YAxis> {
122+
return layoutItem(bottomLayoutGuide, .Top)
96123
}
97124

98-
public var left: LayoutItem<XAxis> { return layoutItem(.Left) }
99-
public var right: LayoutItem<XAxis> { return layoutItem(.Right) }
100-
public var top: LayoutItem<YAxis> { return layoutItem(.Top) }
101-
public var bottom: LayoutItem<YAxis> { return layoutItem(.Bottom) }
102-
public var leading: LayoutItem<XAxis> { return layoutItem(.Leading) }
103-
public var trailing: LayoutItem<XAxis> { return layoutItem(.Trailing) }
104-
public var width: LayoutItem<Dimension> { return layoutItem(.Width) }
105-
public var height: LayoutItem<Dimension> { return layoutItem(.Height) }
106-
public var centerX: LayoutItem<XAxis> { return layoutItem(.CenterX) }
107-
public var centerY: LayoutItem<YAxis> { return layoutItem(.CenterY) }
108-
public var baseline: LayoutItem<YAxis> { return layoutItem(.Baseline) }
125+
public var bottomLayoutGuideBottom: LayoutItem<YAxis> {
126+
return layoutItem(bottomLayoutGuide, .Bottom)
127+
}
109128
}
129+
#endif
110130

111131
infix operator ~ { associativity left precedence 120 }
112132

SwiftAutoLayoutTests/SwiftAutoLayoutTests.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
import XCTest
55
import SwiftAutoLayout
66

7+
#if os(iOS)
8+
private class ViewController: UIViewController {
9+
private override func loadView() {
10+
self.view = UIView(frame: CGRectZero)
11+
}
12+
}
13+
#endif
14+
715
class SwiftAutoLayoutTests: XCTestCase {
816
let view1 = View(frame: CGRectZero)
917
let view2 = View(frame: CGRectZero)
1018

11-
func testAttributeValues() {
19+
func testViewAttributeValues() {
1220
XCTAssertEqual(view1.left.attribute, NSLayoutAttribute.Left)
1321
XCTAssertEqual(view1.right.attribute, NSLayoutAttribute.Right)
1422
XCTAssertEqual(view1.top.attribute, NSLayoutAttribute.Top)
@@ -22,6 +30,17 @@ class SwiftAutoLayoutTests: XCTestCase {
2230
XCTAssertEqual(view1.baseline.attribute, NSLayoutAttribute.Baseline)
2331
}
2432

33+
#if os(iOS)
34+
func testViewControllerAttributeValues() {
35+
let viewController = ViewController()
36+
print(viewController.view) // Load the view
37+
XCTAssertEqual(viewController.topLayoutGuideTop.attribute, NSLayoutAttribute.Top)
38+
XCTAssertEqual(viewController.topLayoutGuideBottom.attribute, NSLayoutAttribute.Bottom)
39+
XCTAssertEqual(viewController.bottomLayoutGuideTop.attribute, NSLayoutAttribute.Top)
40+
XCTAssertEqual(viewController.bottomLayoutGuideBottom.attribute, NSLayoutAttribute.Bottom)
41+
}
42+
#endif
43+
2544
func testEqual() {
2645
let constraint = view1.left == view2.right;
2746
XCTAssertEqual(constraint.firstItem as? View, view1)

0 commit comments

Comments
 (0)