Skip to content

Instantly share code, notes, and snippets.

@felixvisee
Created July 9, 2015 14:03
Show Gist options
  • Save felixvisee/3b945e18f7373c1915b8 to your computer and use it in GitHub Desktop.
Save felixvisee/3b945e18f7373c1915b8 to your computer and use it in GitHub Desktop.
A `Box`-like struct returning a value based on the current traits of a view.
public class View: UIView {
public override var traitCollection: UITraitCollection {
return UITraitCollection(traitsFromCollections: [
UITraitCollection(horizontalSizeClass: .Compact),
UITraitCollection(verticalSizeClass: .Compact)
])
}
}
let view = View()
let variable = Variable(view: view, definition: [
(UITraitCollection(horizontalSizeClass: .Compact), 1),
(UITraitCollection(horizontalSizeClass: .Unspecified), 5)
])
variable.value
import UIKit
public struct Variable<T> {
public var view: UIView
public var definition: [(traitCollection: UITraitCollection, value: T)]
public var value: T? {
for (traitCollection, value) in definition {
if view.traitCollection.containsTraitsInCollection(traitCollection) {
return value
}
}
return nil
}
}
@felixvisee
Copy link
Author

Much easier:

let variable = Variable(view: view, definition: [
    (UITraitCollection(traitsFromCollections: [ UITraitCollection(horizontalSizeClass: .Compact), UITraitCollection(verticalSizeClass: .Compact) ]), (width: 10, height: 10)),
    (UITraitCollection(traitsFromCollections: [ UITraitCollection(horizontalSizeClass: .Regular), UITraitCollection(verticalSizeClass: .Regular) ]), (width: 30, height: 30))
])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment