Created
July 9, 2015 14:03
-
-
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.
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
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 |
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 | |
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 | |
} | |
} |
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
To make my concerns more clear: A view is defined by a vertical and a horizontal size class.
The definition on Variable only returns a value considering one part of the trait collection.
This returns (10, 10), which is not really correct.
What about something like:
Maybe even make the second traitcollection optional?