SimpleLayout
SimpleLayout 帮助您轻松使用自动布局
示例
为了运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
代码中的差异
使用纯 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()
使用 SimpleLayout 进行自动布局
view.layout
.top(-10)
.trailing(10)
.width(fixed: 30)
.height(fixed: 30)
使用 SafeAreaLayoutGuide 适配 iPhone X (iOS 11 及以上)
view.layout
.top(by: view._safeAreaLayoutGuide)
.leading()
.trailing()
.bottom(by: view._safeAreaLayoutGuide)
使用 fill 方法实现自动布局
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 及以上
安装
SimpleLayout 可通过 CocoaPods 获取。要安装它,只需在 Podfile 中添加以下行
pod "SimpleLayout-Swift"
Carthage
Carthage 是一个分散式的依赖管理器,它会构建你的依赖并提供二进制框架。
您可以使用以下命令通过 Homebrew 安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将 SimpleLayout 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "pisces/SimpleLayout" ~> 1.2.1
运行 carthage update
构建框架,然后将构建的 SimpleLayout.framework
拖放到您的 Xcode 项目中。
作者
Steve Kim, [email protected]
许可证
SimpleLayout遵循MIT许可证。更多信息请参阅LICENSE文件。