Latest 0.1
Homepage https://github.com/szotp/Arrange
License MIT
Platforms ios 9.0
Authors

Arrange is a small library that simplifies layout creation of your views. As example:

arrange(
    [.centeredVertically, .left(8), .right(8), .spacing(8)],
    loginTextField,
    passwordTextField
)

arrange method does two things at the same time: adds provided views as subviews, and layouts them. By default subviews will be stacked vertically, and aligned to superviews edges. Default behaviour can be overriden using the first argument, an array of enum cases. For example passing .spacing(8) changes spacing on UIStackView (created internally) to the provided value.

Chaining

arrange methods can be chained together to form complicated view hierarchy:

arrange(
    [.centeredVertically, .left(8), .right(8), .spacing(8)],
    loginTextField,
    passwordTextField,
    UIView().arrange(
        [.horizontal, .equalSizes, .spacing(8)],
        signInButton,
        signUpButton
    )
)
Shared style

If you find yourself using the same arrangement items over and over again, you can save them somewhere and simply reference them in your arrangement code.

let defaultStyle : [ArrangementItem] = 
[.centeredVertically, .left(8), .right(8)]
arrange(
    defaultStyle + [.spacing(8)],
    loginTextField,
    passwordTextField
)
Extending

ArrangementItem enum contains after(Arrangement.Closure) which can be used to create custom items. To do that, simply create extension of ArrangementItem and static variable, or static method.

Note: after means that closure will be executed after main arrangement has been completed.

extension ArrangementItem {
    static var hidden : ArrangementItem {
        return after({ context in
            context.superview.isHidden = true
        })
    }
}

Then, you can simply use your extension as any other item:

view.arrange([.hidden])

Example

Check the ArrangePlayground.playground file for sample code. Use timeline feature in Xcode to see the changes in real time.

How to install

Carthage

Add the following line to your Cartfile:

github "szotp/Arrange"

CocoaPods

Add the following line to your Podfile:

pod "Arrange", :git => "https://github.com/szotp/Arrange"

Manully

Copy Layout.swift file to your project.

TODOs

  • [ ] handling of layout guides
  • [ ] support for animations
  • [ ] better support for customizations, for example getting reference to the created stackView

Latest podspec

{
    "name": "Arrange",
    "version": "0.1",
    "summary": "Little helper library for programmatic autolayout",
    "homepage": "https://github.com/szotp/Arrange",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "szotp": "[email protected]"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/szotp/Arrange.git",
        "tag": 0.1
    },
    "source_files": "Arrange/*.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This