GitHub - MichalTKwiecien/Layoutable: Extension for UIView that make use of Auto...

 4 years ago
source link: https://github.com/MichalTKwiecien/Layoutable
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Layoutable is an extension for UIView apps that make use of Auto Layout easier. This framework doesn't replace any existing Auto Layout API available in UIKit. Anchor API is very good and easy to use, but sometimes require a lot of code repetition for common tasks. This framework only adds a couple of useful extensions. You can check the implementation in just one single file.

Usage & features


Here's how the code would look like when constraining the screen above. See the example for the full version.

Just UIKit

import UIKit

final class ViewController: UIViewController {

    private let headerView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.translatesAutoresizingMaskIntoConstraints = false
        return view


    override func loadView() {

            headerView.topAnchor.constraint(equalTo: view.topAnchor),
            headerView.leftAnchor.constraint(equalTo: view.leftAnchor),
            headerView.rightAnchor.constraint(equalTo: view.rightAnchor),
            headerView.heightAnchor.constraint(equalToConstant: 200),

            headerCenterView.widthAnchor.constraint(equalToConstant: 80),
            headerCenterView.heightAnchor.constraint(equalToConstant: 80),
            headerCenterView.centerXAnchor.constraint(equalTo: headerView.centerXAnchor),
            headerCenterView.centerYAnchor.constraint(equalTo: headerView.centerYAnchor),

            contentContainerView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
            contentContainerView.leftAnchor.constraint(equalTo: view.leftAnchor),
            contentContainerView.rightAnchor.constraint(equalTo: view.rightAnchor),
            contentContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor),

            contentView.topAnchor.constraint(equalTo: contentContainerView.topAnchor, constant: 80),
            contentView.leftAnchor.constraint(equalTo: contentContainerView.leftAnchor, constant: 20),
            contentView.rightAnchor.constraint(equalTo: contentContainerView.rightAnchor, constant: -20),
            contentView.bottomAnchor.constraint(equalTo: contentContainerView.bottomAnchor, constant: -20),

UIKit + Layoutable

import UIKit
import Layoutable

final class ViewController: UIViewController {

    private let headerView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view.layoutable()


    override func loadView() {

        headerView.constrainToSuperviewEdges(excluding: [.bottom])
        headerView.heightAnchor.constraint(equalToConstant: 200).isActive = true

        headerCenterView.constrainToConstant(size: .init(width: 80, height: 80))

        contentContainerView.constrainToSuperviewEdges(excluding: [.top])
        contentContainerView.topAnchor.constraint(equalTo: headerView.bottomAnchor).isActive = true

        contentView.constrainToSuperviewEdges(insets: .init(top: 80, left: 20, bottom: 20, right: 20))

Detecting if view can be used with Auto Layout

For all of the views that you want to use with Auto Layout you have to type translatesAutoresizingMaskIntoConstraints = false. It's very easy to forget and have a lot of conflicts after running your app. Layoutable checks for this situation and informs you how to fix it.


Layoutable supports iOS 9.0 or higher.



If you're using Carthage, add the following dependency to your Cartfile:

github "MichalTKwiecien/Layoutable" {version}


If you're using CocoaPods, add the following dependency to your Podfile:

pod 'Layoutable', '~> {version}'


You can just drag and drop single file to your project.


Development requires following tools:

After cloning the repository, install project's dependencies:

$ carthage bootstrap --platform iOS

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK