-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from TimothyChilvers/master
Add UILayoutSupport support
- Loading branch information
Showing
6 changed files
with
294 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// LayoutSupport.swift | ||
// Cartography | ||
// | ||
// Created by Timothy Chilvers on 30/03/2016. | ||
// Copyright © 2016 Robert Böhnke. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
#if os(iOS) || os(tvOS) | ||
import UIKit | ||
|
||
public struct LayoutSupport { | ||
let layoutGuide : UILayoutSupport | ||
let attribute : NSLayoutAttribute | ||
} | ||
|
||
public extension UIViewController { | ||
|
||
public var topLayoutGuideCartography : LayoutSupport { | ||
get { | ||
return LayoutSupport(layoutGuide: self.topLayoutGuide, attribute: .Bottom) | ||
} | ||
} | ||
|
||
public var bottomLayoutGuideCartography : LayoutSupport { | ||
get { | ||
return LayoutSupport(layoutGuide: self.bottomLayoutGuide, attribute: .Top) | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import Cartography | ||
|
||
import Nimble | ||
import Quick | ||
import UIKit | ||
|
||
class LayoutSupportSpec: QuickSpec { | ||
override func spec() { | ||
var window: TestWindow! | ||
var view: TestView! | ||
var viewController: UIViewController! | ||
var navigationController: UINavigationController! | ||
var tabBarController: UITabBarController! | ||
|
||
beforeEach { | ||
window = TestWindow(frame: CGRectMake(0,0,400,400)) | ||
|
||
view = TestView(frame: CGRectZero) | ||
|
||
viewController = UIViewController() | ||
viewController.view.addSubview(view) | ||
|
||
constrain(view) { view in | ||
view.height == 200 | ||
view.width == 200 | ||
} | ||
|
||
navigationController = UINavigationController(rootViewController: viewController) | ||
tabBarController = UITabBarController() | ||
tabBarController.viewControllers = [navigationController] | ||
tabBarController.view.frame = window.bounds | ||
tabBarController.view.layoutIfNeeded() | ||
window.rootViewController = tabBarController | ||
|
||
window.setNeedsLayout() | ||
window.layoutIfNeeded() | ||
|
||
print(viewController.topLayoutGuide.debugDescription) | ||
} | ||
|
||
describe("LayoutSupport.top") { | ||
it("should support relative equalities") { | ||
|
||
viewController.view.layoutIfNeeded() | ||
|
||
constrain(view) { view in | ||
view.top == viewController.topLayoutGuideCartography | ||
} | ||
viewController.view.layoutIfNeeded() | ||
|
||
expect(view.convertRect(view.bounds, toView: window).minY).to(equal(viewController.topLayoutGuide.length)) | ||
} | ||
|
||
it("should support relative inequalities") { | ||
constrain(view) { view in | ||
view.top <= viewController.topLayoutGuideCartography | ||
view.top >= viewController.topLayoutGuideCartography | ||
} | ||
|
||
viewController.view.layoutIfNeeded() | ||
|
||
expect(view.convertRect(view.bounds, toView: window).minY).to(equal(viewController.topLayoutGuide.length)) | ||
} | ||
|
||
it("should support addition") { | ||
constrain(view) { view in | ||
view.top == viewController.topLayoutGuideCartography + 100 | ||
} | ||
|
||
viewController.view.layoutIfNeeded() | ||
|
||
expect(view.convertRect(view.bounds, toView: window).minY).to(equal(100 + viewController.topLayoutGuide.length)) | ||
} | ||
|
||
it("should support subtraction") { | ||
constrain(view) { view in | ||
view.top == viewController.topLayoutGuideCartography - 100 | ||
} | ||
|
||
viewController.view.layoutIfNeeded() | ||
|
||
expect(view.convertRect(view.bounds, toView: window).minY).to(equal(-100 - viewController.topLayoutGuide.length)) | ||
} | ||
} | ||
|
||
describe("LayoutSupport.bottom") { | ||
it("should support relative equalities") { | ||
constrain(view) { view in | ||
view.bottom == viewController.bottomLayoutGuideCartography | ||
} | ||
viewController.view.layoutIfNeeded() | ||
|
||
expect(view.convertRect(view.bounds, toView: window).maxY).to(equal(window.bounds.maxY - viewController.bottomLayoutGuide.length)) | ||
} | ||
|
||
it("should support relative inequalities") { | ||
constrain(view) { view in | ||
view.bottom <= viewController.bottomLayoutGuideCartography | ||
view.bottom >= viewController.bottomLayoutGuideCartography | ||
} | ||
|
||
viewController.view.layoutIfNeeded() | ||
|
||
expect(view.convertRect(view.bounds, toView: window).maxY).to(equal(window.bounds.maxY - viewController.bottomLayoutGuide.length)) | ||
} | ||
|
||
it("should support addition") { | ||
constrain(view) { view in | ||
view.bottom == viewController.bottomLayoutGuideCartography + 100 | ||
} | ||
|
||
viewController.view.layoutIfNeeded() | ||
|
||
expect(view.convertRect(view.bounds, toView: window).maxY).to(equal(100 + window.bounds.maxY - viewController.bottomLayoutGuide.length)) | ||
} | ||
|
||
it("should support subtraction") { | ||
constrain(view) { view in | ||
view.bottom == viewController.bottomLayoutGuideCartography - 100 | ||
} | ||
|
||
viewController.view.layoutIfNeeded() | ||
|
||
expect(view.convertRect(view.bounds, toView: window).maxY).to(equal((window.bounds.maxY - 100) - viewController.bottomLayoutGuide.length)) | ||
} | ||
|
||
} | ||
|
||
} | ||
} |