mirror of
https://github.com/wnagrodzki/UserInterfaceVariations.git
synced 2025-04-04 19:31:59 +02:00
No description
.swiftpm/xcode | ||
Sources/UserInterfaceVariations | ||
Tests/UserInterfaceVariationsTests | ||
.gitignore | ||
LICENSE | ||
Package.swift | ||
README.md |
UserInterfaceVariations
Allows defining user interface variations depending on UIUserInterfaceSizeClass
. It is a substitute of functionality provided by Xcode Interface Builder.
Example usages
Deactivate NSLayoutConstraint
for compact horizontal size class:
addVariation(for: constraint,
property: \.isActive,
sizeClassDimension: .horizontal,
whenCompact: false)
Control UILabel
text
in every size class configuration:
addVariations([
UIVariation(object: label,
keyPath: \.text,
value: "w:C h:C",
horizontalSizeClass: .compact,
verticalSizeClass: .compact),
UIVariation(object: label,
keyPath: \.text,
value: "w:C h:R",
horizontalSizeClass: .compact,
verticalSizeClass: .regular),
UIVariation(object: label,
keyPath: \.text,
value: "w:R h:C",
horizontalSizeClass: .regular,
verticalSizeClass: .compact),
UIVariation(object: label,
keyPath: \.text,
value: "w:R h:R",
horizontalSizeClass: .regular,
verticalSizeClass: .regular)
])
Make only horizontal class matter:
addVariations([
UIVariation(object: label,
keyPath: \.text,
value: "w:C h:Any",
horizontalSizeClass: .compact,
verticalSizeClass: nil),
UIVariation(object: label,
keyPath: \.text,
value: "w:R h:Any",
horizontalSizeClass: .regular,
verticalSizeClass: nil),
])
Assign new value to variation:
let variation = UIVariation(object: view,
keyPath: \.backgroundColor,
value: .white,
horizontalSizeClass: .compact,
verticalSizeClass: nil)
variation.value = .black
Integration
Declare UIVariationEnvironment
conformance on an object already conforming to UITraitEnvironment
(e.g. UIViewController
, UIView
) and override following method.
class ViewController: UIViewController, UIVariationEnvironment {
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
activateVariationsMatchingTraitEnvironment()
}
}