Last active
July 13, 2017 06:49
-
-
Save rikumi/7b44f13f7d231b67c649a81ab4e3cf22 to your computer and use it in GitHub Desktop.
SnapKit 扩展插件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Vhyme on 2017/7/13. | |
// | |
/* Usage Example: | |
func layoutCourseData() { | |
coursePage.removeAllSubviews() | |
// 遍历课程数据 | |
for course in courseData { | |
// 课程总控件 | |
let view = UIView() | |
view.into(coursePage).left().right().width().flowDown() | |
// 图片 | |
let image = UIImageView() | |
image.contentMode = .scaleAspectFill | |
image.clipsToBounds = true | |
image.sd_setImage(with: URL(string: course.image)) | |
image.into(view).top().left().right().ratio(375/200) | |
// 标题文字 | |
let title = UILabel() | |
title.text = course.title | |
title.into(view).left(20).right(20).below(image, 15) | |
// 介绍文字 | |
let summary = UILabel() | |
summary.text = course.summary | |
summary.into(view).left(20).right(20).bottom(20).below(title, 5) | |
} | |
} | |
*/ | |
import UIKit | |
import SnapKit | |
extension UIView { | |
/// 添加到某个父控件 | |
@discardableResult func into(_ superview: UIView) -> UIView { | |
self.removeFromSuperview() | |
superview.addSubview(self) | |
return self | |
} | |
/// 清除自身的约束 | |
@discardableResult func remake() -> UIView { | |
self.snp.removeConstraints() | |
return self | |
} | |
/// 令宽度等于常数 | |
@discardableResult func width(_ const: CGFloat) -> UIView { | |
self.snp.makeConstraints { | |
$0.width.equalTo(const) | |
} | |
return self | |
} | |
/// 令高度等于常数 | |
@discardableResult func height(_ const: CGFloat) -> UIView { | |
self.snp.makeConstraints { | |
$0.height.equalTo(const) | |
} | |
return self | |
} | |
/// 令宽度、高度等于 CGSize | |
@discardableResult func size(_ size: CGSize) -> UIView { | |
self.snp.makeConstraints { | |
$0.width.equalTo(size.width) | |
$0.height.equalTo(size.height) | |
} | |
return self | |
} | |
/// 令宽高比等于常数 | |
@discardableResult func ratio(_ const: CGFloat) -> UIView { | |
self.snp.makeConstraints { | |
$0.width.equalTo(self.snp.height).multipliedBy(const) | |
} | |
return self | |
} | |
/// 令左边贴紧父控件,可指定边距 | |
@discardableResult func left(_ inset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.left.equalToSuperview().offset(inset) | |
} | |
return self | |
} | |
/// 令右边贴紧父控件,可指定边距 | |
@discardableResult func right(_ inset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.right.equalToSuperview().offset(-inset) | |
} | |
return self | |
} | |
/// 令上边贴紧父控件,可指定边距 | |
@discardableResult func top(_ inset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.top.equalToSuperview().offset(inset) | |
} | |
return self | |
} | |
/// 令下边贴紧父控件,可指定边距 | |
@discardableResult func bottom(_ inset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.bottom.equalToSuperview().offset(-inset) | |
} | |
return self | |
} | |
/// 令四边贴紧父控件,可指定边距 | |
@discardableResult func insets(_ inset: UIEdgeInsets) -> UIView { | |
self.snp.makeConstraints { | |
$0.edges.equalToSuperview().inset(inset) | |
} | |
return self | |
} | |
/// 令宽度等于父控件宽度 | |
@discardableResult func width() -> UIView { | |
self.snp.makeConstraints { | |
$0.width.equalToSuperview() | |
} | |
return self | |
} | |
/// 令高度等于父控件高度 | |
@discardableResult func height() -> UIView { | |
self.snp.makeConstraints { | |
$0.height.equalToSuperview() | |
} | |
return self | |
} | |
/// 与父控件左右居中对齐,可指定偏移,右移为正,左移为负 | |
@discardableResult func centerX(_ offset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.centerX.equalToSuperview().offset(offset) | |
} | |
return self | |
} | |
/// 与父控件上下居中对齐,可指定偏移,下移为正,上移为负 | |
@discardableResult func centerY(_ offset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.centerY.equalToSuperview().offset(offset) | |
} | |
return self | |
} | |
/// 与父控件居中对齐,可指定偏移,右下移为正,左上移为负 | |
@discardableResult func center(_ offsetX: CGFloat = 0, _ offsetY: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.centerX.equalToSuperview().offset(offsetX) | |
$0.centerY.equalToSuperview().offset(offsetY) | |
} | |
return self | |
} | |
/// 令左边与另一控件左边对齐,可指定偏移,右移为正,左移为负 | |
@discardableResult func left(_ view: UIView, _ offset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.left.equalTo(view).offset(offset) | |
} | |
return self | |
} | |
/// 令右边与另一控件右边对齐,可指定偏移,右移为正,左移为负 | |
@discardableResult func right(_ view: UIView, _ offset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.right.equalTo(view).offset(offset) | |
} | |
return self | |
} | |
/// 令上边与另一控件上边对齐,可指定偏移,下移为正,上移为负 | |
@discardableResult func top(_ view: UIView, _ offset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.top.equalTo(view).offset(offset) | |
} | |
return self | |
} | |
/// 令下边与另一控件下边对齐,可指定偏移,下移为正,上移为负 | |
@discardableResult func bottom(_ view: UIView, _ offset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.bottom.equalTo(view).offset(offset) | |
} | |
return self | |
} | |
/// 令宽度与另一控件相等,可指定比例系数 | |
@discardableResult func width(_ view: UIView, _ multiplier: CGFloat = 1) -> UIView { | |
self.snp.makeConstraints { | |
$0.width.equalTo(view).multipliedBy(multiplier) | |
} | |
return self | |
} | |
/// 令高度与另一控件相等,可指定比例系数 | |
@discardableResult func height(_ view: UIView, _ multiplier: CGFloat = 1) -> UIView { | |
self.snp.makeConstraints { | |
$0.height.equalTo(view).multipliedBy(multiplier) | |
} | |
return self | |
} | |
/// 与另一控件左右居中对齐,可指定偏移,右移为正,左移为负 | |
@discardableResult func centerX(_ view: UIView, _ offset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.centerX.equalTo(view).offset(offset) | |
} | |
return self | |
} | |
/// 与另一控件上下居中对齐,可指定偏移,下移为正,上移为负 | |
@discardableResult func centerY(_ view: UIView, _ offset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.centerY.equalTo(view).offset(offset) | |
} | |
return self | |
} | |
/// 紧接在另一控件右侧,可指定间距 | |
@discardableResult func after(_ view: UIView, _ spacing: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.left.equalTo(view.snp.right).offset(spacing) | |
} | |
return self | |
} | |
/// 紧接在另一控件左侧,可指定间距 | |
@discardableResult func before(_ view: UIView, _ spacing: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.right.equalTo(view.snp.left).offset(-spacing) | |
} | |
return self | |
} | |
/// 紧接在另一控件下方,可指定间距 | |
@discardableResult func below(_ view: UIView, _ spacing: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.top.equalTo(view.snp.bottom).offset(spacing) | |
} | |
return self | |
} | |
/// 紧接在另一控件上方,可指定间距 | |
@discardableResult func above(_ view: UIView, _ spacing: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.bottom.equalTo(view.snp.top).offset(-spacing) | |
} | |
return self | |
} | |
/// 向上撑起父控件,即令控件上边至少在父控件上边的下方 | |
@discardableResult func inflateUp(_ inset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.top.greaterThanOrEqualToSuperview().offset(inset) | |
} | |
return self | |
} | |
/// 向上撑起父控件,即令控件下边至少在父控件下边的上方 | |
@discardableResult func inflateDown(_ inset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.bottom.lessThanOrEqualToSuperview().offset(-inset) | |
} | |
return self | |
} | |
/// 向左撑起父控件,即令控件左边至少在父控件左边的右方 | |
@discardableResult func inflateLeft(_ inset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.left.greaterThanOrEqualToSuperview().offset(inset) | |
} | |
return self | |
} | |
/// 向右撑起父控件,即令控件右边至少在父控件右边的左方 | |
@discardableResult func inflateRight(_ inset: CGFloat = 0) -> UIView { | |
self.snp.makeConstraints { | |
$0.right.lessThanOrEqualToSuperview().offset(-inset) | |
} | |
return self | |
} | |
/// 自动向下排列,即自动寻找父控件中上一个控件,紧贴在其下方,并自动向下撑起父控件 | |
@discardableResult func flowDown(_ spacing: CGFloat = 0) -> UIView { | |
let lastView = superview?.subviews.filter { $0 != self }.last | |
if let lastView = lastView { | |
below(lastView, spacing) | |
} else { | |
top() | |
} | |
inflateDown() | |
return self | |
} | |
/// 自动向右排列,即自动寻找父控件中上一个控件,紧贴在其右方,并自动向右撑起父控件 | |
@discardableResult func flowRight(_ spacing: CGFloat = 0) -> UIView { | |
let lastView = superview?.subviews.filter { $0 != self }.last | |
if let lastView = lastView { | |
after(lastView, spacing) | |
} else { | |
left() | |
} | |
inflateRight() | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment