NorthLayout
代码中自动布局视图的快捷方式
安装
NorthLayout通过CocoaPods提供。要安装它,只需在您的Podfile中添加以下行:
pod "NorthLayout"
使用方法
view.northLayoutFormat(_:_:)
获取视图的自动布局闭包autolayout(...)
使用 Autolayout视觉格式语言进行布局- 不要求使用storyboards
(另见 Example
)
override func loadView() {
super.loadView()
view.backgroundColor = .whiteColor()
let iconView = UIImageView(image: colorImage(UIColor(red: 0.63, green: 0.9, blue: 1, alpha: 1)))
let iconWidth = CGFloat(32)
iconView.layer.cornerRadius = iconWidth / 2
iconView.clipsToBounds = true
let nameLabel = UILabel()
nameLabel.text = "Name"
let dateLabel = UILabel()
dateLabel.text = "1 min ago"
dateLabel.font = UIFont.systemFontOfSize(12)
dateLabel.textColor = UIColor.lightGrayColor()
let textLabel = UILabel()
textLabel.text = "Some text go here"
let favButton = UIButton(type: .System)
favButton.setTitle("⭐️", forState: .Normal)
favButton.backgroundColor = UIColor(red: 0.17, green: 0.29, blue: 0.45, alpha: 1.0)
favButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
favButton.layer.cornerRadius = 4
favButton.clipsToBounds = true
let replyButton = UIButton(type: .System)
replyButton.setTitle("Reply", forState: .Normal)
replyButton.backgroundColor = favButton.backgroundColor
replyButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
replyButton.layer.cornerRadius = 4
replyButton.clipsToBounds = true
let autolayout = northLayoutFormat(["p": 8, "iconWidth": iconWidth], [
"icon": iconView,
"name": nameLabel,
"date": dateLabel,
"text": textLabel,
"fav": favButton,
"reply": replyButton,
])
autolayout("H:|-p-[icon(==iconWidth)]-p-[name]-p-[date]-p-|")
autolayout("H:|-p-[text]-p-|")
autolayout("H:|-p-[fav]-p-[reply(==fav)]-p-|")
autolayout("V:|-p-[icon(==iconWidth)]-p-[text]")
autolayout("V:|-p-[name(==icon)]")
autolayout("V:|-p-[date]")
autolayout("V:[text]-p-[fav]")
autolayout("V:[text]-p-[reply]")
}
代码生成布局
带有约束
名称
NorthLayout 命名取自其 cocoapodized 的位置,即日本北海道 湖地,是占星术的背景。
许可证
NorthLayout 内存下 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。