Latest 0.1.0
Homepage https://github.com/Eckelf/SizeClasses
License MIT
Platforms ios 8.0
Authors

CI Status
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 CocoaPods. To install
it, simply add the following line to your Podfile:

pod "SizeClasses"

Example

To run the example project, run pod try SizeClasses or clone the repo, and run pod install from the Example directory first.

Author

Vincent Flecke, [email protected]

License

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

Latest podspec

{
    "name": "SizeClasses",
    "version": "0.1.0",
    "summary": "Use size classes in programmatic layouts effectively.",
    "homepage": "https://github.com/Eckelf/SizeClasses",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Vincent Flecke": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Eckelf/SizeClasses.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "SizeClasses/Classes/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This