SimpleLayout
SimpleLayout helps you to using auto layout very easily
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Difference in code
Using auto layout with pure iOS SDK
let top = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: -10)
let trailing = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 10)
let width = NSLayoutConstraint(item: view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
let height = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
view.addConstraints([width, height])
view.layoutIfNeeded()
self.addConstraints([top, trailing])
self.layoutIfNeeded()
Using auto layout with SimpleLayout
view.layout
.top(-10)
.trailing(10)
.width(fixed: 30)
.height(fixed: 30)
Using SafeAreaLayoutGuide for iPhone X (iOS 11 or higher)
view.layout
.top(by: view._safeAreaLayoutGuide)
.leading()
.trailing()
.bottom(by: view._safeAreaLayoutGuide)
To implement auto layout using the fill method
import UIKit
import SimpleLayout_Swift
class FillExampleViewController: UIViewController {
private lazy var subview: UIView = {
let label = UILabel()
label.text = "subview"
label.textColor = .white
let subview = UIView()
subview.backgroundColor = .red
subview.addSubview(label)
label.layout
.leading()
.top()
.width(fixed: 0, relation: .greaterThanOrEqual)
.height(fixed: 0, relation: .greaterThanOrEqual)
return subview
}()
override func viewDidLoad() {
super.viewDidLoad()
edgesForExtendedLayout = .bottom
let label = UILabel()
label.text = "view"
view.addSubview(label)
view.addSubview(subview)
label.layout
.leading()
.top()
.width(fixed: 0, relation: .greaterThanOrEqual)
.height(fixed: 0, relation: .greaterThanOrEqual)
subview.layout.fill(leading: 30, top: 30, trailing: -30, bottom: -30)
}
}
To implement auto layout using the property chain
import UIKit
import SimpleLayout_Swift
class ChainExampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
edgesForExtendedLayout = .bottom
let subview1 = label(backgroundColor: .red, text: "subview1")
let subview2 = label(backgroundColor: .purple, text: "subview2")
let subview3 = label(backgroundColor: .blue, text: "subview3")
view.addSubview(subview1)
view.addSubview(subview2)
view.addSubview(subview3)
subview1.layout
.leading()
.top()
.width()
.height(fixed: 150)
subview2.layout
.leading(by: subview1)
.top(by: subview1, attribute: .bottom)
.width(fixed: view.width/2)
.bottom()
subview3.layout
.leading(by: subview2, attribute: .trailing)
.top(by: subview2)
.trailing()
.bottom(by: view.layout.safeAreaLayoutGuide)
}
private func label(backgroundColor: UIColor, text: String) -> UILabel {
let label = UILabel()
label.backgroundColor = backgroundColor
label.text = text
label.textAlignment = .center
label.textColor = .white
return label
}
}
Requirements
iOS SDK 9.0 equal or higher
Installation
SimpleLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SimpleLayout-Swift"
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate SimpleLayout into your Xcode project using Carthage, specify it in your Cartfile
:
github "pisces/SimpleLayout" ~> 1.2.1
Run carthage update
to build the framework and drag the built SimpleLayout.framework
into your Xcode project.
Author
Steve Kim, [email protected]
License
SimpleLayout is available under the MIT license. See the LICENSE file for more info.