Last active
March 16, 2016 11:34
-
-
Save adiki/b13ac90b6198c84e8fc8 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
import UIKit | |
class SizeClassView: UIView { | |
private var regularHeightConstraints = [NSLayoutConstraint]() | |
private var compactHeigthConstraints = [NSLayoutConstraint]() | |
func addRegularHeightConstraint(regularHeightConstraint: NSLayoutConstraint) { | |
regularHeightConstraints.append(regularHeightConstraint) | |
addConstraint(regularHeightConstraint) | |
regularHeightConstraint.active = false | |
} | |
func addRegularHeightConstraints(regularHeightConstraints: [NSLayoutConstraint]) { | |
self.regularHeightConstraints.appendContentsOf(regularHeightConstraints) | |
addConstraints(regularHeightConstraints) | |
NSLayoutConstraint.deactivateConstraints(regularHeightConstraints) | |
} | |
func addCompactHeightConstraint(compactHeigthConstraint: NSLayoutConstraint) { | |
compactHeigthConstraints.append(compactHeigthConstraint) | |
addConstraint(compactHeigthConstraint) | |
compactHeigthConstraint.active = false | |
} | |
func addCompactHeightConstraints(compactHeigthConstraints: [NSLayoutConstraint]) { | |
self.compactHeigthConstraints.appendContentsOf(compactHeigthConstraints) | |
addConstraints(compactHeigthConstraints) | |
NSLayoutConstraint.deactivateConstraints(compactHeigthConstraints) | |
} | |
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { | |
super.traitCollectionDidChange(previousTraitCollection) | |
switch traitCollection.verticalSizeClass { | |
case .Unspecified: | |
break | |
case .Regular: | |
NSLayoutConstraint.deactivateConstraints(compactHeigthConstraints) | |
NSLayoutConstraint.activateConstraints(regularHeightConstraints) | |
case .Compact: | |
NSLayoutConstraint.deactivateConstraints(regularHeightConstraints) | |
NSLayoutConstraint.activateConstraints(compactHeigthConstraints) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment