No description
Find a file
2021-06-09 17:14:51 +02:00
.swiftpm/xcode Enable code coverage for all targets 2021-06-03 10:16:16 +02:00
Sources/UserInterfaceVariations Add addVariations(_:) convenience method to UIVariationEnvironment 2021-06-09 16:38:26 +02:00
Tests/UserInterfaceVariationsTests Add addVariations(_:) convenience method to UIVariationEnvironment 2021-06-09 16:38:26 +02:00
.gitignore Initial Commit 2021-06-02 22:54:33 +02:00
LICENSE Add license 2021-06-09 17:14:51 +02:00
Package.swift Set minimum deployment target version for the iOS platform to 12 2021-06-03 10:14:21 +02:00
README.md Update readme 2021-06-09 17:11:56 +02:00

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()
    }
}