Last active
August 29, 2015 14:14
-
-
Save felixvisee/c85940ed9036a47f1fe5 to your computer and use it in GitHub Desktop.
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
// | |
// AdapativeConstraintGroup.swift | |
// CartographyDemo | |
// | |
// Created by Felix Jendrusch on 2/3/15. | |
// Copyright (c) 2015 Felix Jendrusch. All rights reserved. | |
// | |
import Cartography | |
public class AdaptiveConstraintGroup: ConstraintGroup { | |
public var traitCollection: UITraitCollection | |
public init(traitCollection: UITraitCollection) { | |
self.traitCollection = traitCollection | |
} | |
public class func activateConstraintsInGroups(groups: [AdaptiveConstraintGroup], ifTraitsContainedInCollection traitCollection: UITraitCollection) { | |
// Deactivate all unwated constraints before activating any new ones to | |
// avoid possible conflicts. | |
for group in groups { | |
if !traitCollection.containsTraitsInCollection(group.traitCollection) { | |
group.active = false | |
} | |
} | |
for group in groups { | |
if traitCollection.containsTraitsInCollection(group.traitCollection) { | |
group.active = true | |
} | |
} | |
} | |
public func activateConstraintsIfTraitsContainedInCollection(traitCollection: UITraitCollection) { | |
self.active = traitCollection.containsTraitsInCollection(self.traitCollection) | |
} | |
} | |
public func adapt(view: UIView, traitCollection: UITraitCollection, block: LayoutProxy -> ()) -> AdaptiveConstraintGroup { | |
let group = AdaptiveConstraintGroup(traitCollection: traitCollection) | |
constrain(view, replace: group, block) | |
// Deactivate new constraints to avoid possible conflicts with existing ones. | |
group.active = false | |
return group | |
} |
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
// | |
// ViewController.swift | |
// CartographyDemo | |
// | |
// Created by Felix Jendrusch on 2/3/15. | |
// Copyright (c) 2015 Felix Jendrusch. All rights reserved. | |
// | |
import UIKit | |
import Cartography | |
class ViewController: UIViewController { | |
var box: UIView! | |
var groups: [AdaptiveConstraintGroup] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
box = UIView(frame: CGRectMake(0, 0, 100, 100)) | |
box.backgroundColor = UIColor.redColor() | |
view.addSubview(box) | |
} | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
layout(box) { box in | |
box.width == 100 | |
box.height == 100 | |
} | |
groups.append(adapt(box, UITraitCollection(verticalSizeClass: .Compact)) { box in | |
box.top == box.superview!.top | |
box.centerX == box.superview!.centerX | |
}) | |
groups.append(adapt(box, UITraitCollection(verticalSizeClass: .Regular)) { box in | |
box.center == box.superview!.center; return | |
}) | |
AdaptiveConstraintGroup.activateConstraintsInGroups(groups, ifTraitsContainedInCollection: traitCollection) | |
} | |
override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { | |
super.willTransitionToTraitCollection(newCollection, withTransitionCoordinator: coordinator) | |
coordinator.animateAlongsideTransition({ context in | |
AdaptiveConstraintGroup.activateConstraintsInGroups(self.groups, ifTraitsContainedInCollection: self.traitCollection) | |
}, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment