SimpleLayout helps you to using auto layout very easily
To run the example project, clone the repo, and run pod install
from the Example directory first.
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()
view.layout
.top(-10)
.trailing(10)
.width(fixed: 30)
.height(fixed: 30)
view.layout
.top(by: view._safeAreaLayoutGuide)
.leading()
.trailing()
.bottom(by: view._safeAreaLayoutGuide)
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)
}
}
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
}
}
iOS SDK 9.0 equal or higher
SimpleLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SimpleLayout-Swift"
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.
Steve Kim, hh963103@gmail.com
SimpleLayout is available under the MIT license. See the LICENSE file for more info.