Skip to content

Instantly share code, notes, and snippets.

@adiki
Last active March 16, 2016 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adiki/b13ac90b6198c84e8fc8 to your computer and use it in GitHub Desktop.
Save adiki/b13ac90b6198c84e8fc8 to your computer and use it in GitHub Desktop.
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