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.
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
)
) |
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
) |
arrange
called on view controller instead of a view will automatically use layout guides.
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])
Check the ArrangePlayground.playground
file for sample code. Use timeline feature in Xcode to see the changes in real time.
Add the following line to your Cartfile
:
github "szotp/Arrange"
Add the following line to your Podfile
:
pod "Arrange", :git => "https://github.com/szotp/Arrange"
Copy Layout.swift
file to your project.
- handling of layout guides
- support for animations
- better support for customizations, for example getting reference to the created stackView