SizeClasses 0.4.0

SizeClasses 0.4.0

Maintained by Vincent Flecke.



  • By
  • Vincent Flecke

SizeClasses

CI Status Carthage compatible Version License Platform

SizeClasses lets you adapt your programmatic layout to size class changes - use the good parts of storyboards while writing code!

Usage

Let's start with a few examples:

// Base layout is the same in every size class
when(.any, .any, activate: [
    label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])

// Use up the whole screen width in compact horizontal sizes
when(.compact, .any, activate: [
    label.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor),
    view.readableContentGuide.trailingAnchor.constraint(equalTo: label.trailingAnchor)
])

// Use only half of the readable content width in regular horizontal sizes
when(.regular, .any, activate: [
    label.centerXAnchor.constraint(equalTo: view.readableContentGuide.centerXAnchor),
    label.widthAnchor.constraint(equalTo: view.readableContentGuide.widthAnchor, multiplier: 0.5)
])

// Left align label in compact horizontal sizes
when(.compact, .any) {
    label.textAlignment = .left
}

// Center align label in regular horizontal sizes
when(.regular, .any) {
    label.textAlignment = .center
}

Setup

Still interested? Adopt the protocol SizeClasses in any UITraitEnvironment (UIViewController, UIView, etc) and add the following snippet:

class MyViewController: UIViewController, SizeClasses {

    let sizeClassesManager = SizeClassesManager()

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        traitCollectionDidChange()
    }

}

Using in layout

SizeClasses currently supports NSLayoutConstraints and closures.

Similar to Xcode's storyboards there are three size-class choices available:

public enum UserInterfaceSizeClassPredicate {
    case any
    case compact
    case regular
}

The SizeClasses protocol adds several helper functions to its instances:

func when(_ horizontalSizeClass: UserInterfaceSizeClassPredicate, _ verticalSizeClass: UserInterfaceSizeClassPredicate, activate constraints: [NSLayoutConstraint])

func when(_ horizontalSizeClass: UserInterfaceSizeClassPredicate, _ verticalSizeClass: UserInterfaceSizeClassPredicate, do action: @escaping () -> Void) -> Any

func remove(constraint: NSLayoutConstraint)

func remove(actionWith identifier: Any)

Review the code's documentation for more details.

Installation

SizeClasses is available through Carthage. To install it, simply add the following line to your Cartfile:

github "Eckelf/SizeClasses"

SizeClasses is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SizeClasses"

Author

Vincent Flecke, [email protected]

License

SizeClasses is available under the MIT license. See the LICENSE file for more info.