ElegantSnap

ElegantSnap(Base on SnapKit) to make Auto Layout easy and elegant on both iOS and OS X.

基于SnapKit, 用法简洁优雅,可运行在iOS、tvOS、macOS上自动布局库





Requirements / 使用条件

  • iOS 10.0+ / Mac OS X 10.12+ / tvOS 10.0+
  • Xcode 10.2+
  • Swift 5.0+

Installation / 安装

CocoaPods

pod 'ElegantSnap'

Carthage

github "ZuopanYao/ElegantSnap"

Manually / 手动安装

If you prefer not to use either of the aforementioned dependency managers, you can integrate SnapKit into your project manually.

如果您不喜欢以上管理依赖库的方式,则可以手动将 ElegantSnap 集成到项目中。

Usage / 使用

Compare with SnapKit / 与 SnapKit 比较

ElegantSnap SnapKit
aView.make([.top()])
// aView.make([.top(nil, nil)])
// aView.make([.top(nil)])
aView.snp.makeConstraints { (make) in
    make.top.equalToSuperview()
}
aView.make([.top(20)])
// aView.make([.top(nil, 20)])
aView.snp.makeConstraints { (make) in
    make.top.equalToSuperview().offset(20)
}
aView.make([.top(base.snp.bottom)])
// aView.make([.top(base.snp.bottom, nil)])
aView.snp.makeConstraints { (make) in
    make.top.equalTo(base.snp.bottom)
}
aView.make([.top(base.snp.bottom, 20)]) aView.snp.makeConstraints { (make) in
    make.top.equalTo(base.snp.bottom).offset(20)
}
... ...
aView.make([.width()])
// aView.make([.width(nil)])
aView.snp.makeConstraints { (make) in
    make.top.equalToSuperview()
}
aView.make([.width(200)]) aView.snp.makeConstraints { (make) in
    make.width.equalTo(200)
}
aView.make([.width(base.snp.width)]) aView.snp.makeConstraints { (make) in
    make.width.equalTo(base.snp.width)
}
... ...

Quick Start / 快速上手

Example 1 / 示例 1

import ElegantSnap

class ViewController: NSViewController {

    override func viewDidLoad() {
super.viewDidLoad() let aView = NSView()
view.addSubview(aView, constraints: [.top(), .leading(), .width(200), .height(400)]) // view.addSubview(aView)
// aView.make([.top(), .leading(), .width(200), .height(400)])
}
}

equal to / 等同于

import SnapKit

class ViewController: NSViewController {

    override func viewDidLoad() {
super.viewDidLoad() let aView = NSView()
view.addSubview(aView) aView.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.leading.equalToSuperview()
make.width.equalTo(200)
make.height.equalTo(400)
}
}
}

Example 2 / 示例 2

import ElegantSnap

class ViewController: NSViewController {

    override func viewDidLoad() {
super.viewDidLoad() let aView = NSView()
view.addSubview(aView, constraints: [.top(), .leading(), .width(200), .height(400)]) let myView = NSView()
view.addSubview(myView, constraints: [.top(aView.snp.bottom, 20), .leading(), .width(300), .height(aView.snp.height)])
} }

equal to / 等同于

import SnapKit

class ViewController: NSViewController {

    override func viewDidLoad() {
super.viewDidLoad() let aView = NSView()
view.addSubview(aView) aView.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.leading.equalToSuperview()
make.width.equalTo(200)
make.height.equalTo(400)
} let myView = NSView()
view.addSubview(myView) myView.snp.makeConstraints { (make) in
make.top.equalTo(aView.snp.bottom).offset(20)
make.leading.equalToSuperview()
make.width.equalTo(300)
make.height.equalTo(aView.snp.height)
}
}
}

多视图排列

多个View等宽水平排列

import ElegantSnap

class ViewController: NSViewController {

    override func viewDidLoad() {
super.viewDidLoad() let aView = NSView()
aView.wantsLayer = true
aView.layer?.backgroundColor = NSColor.red.cgColor let bView = NSView()
bView.wantsLayer = true
bView.layer?.backgroundColor = NSColor.blue.cgColor let cView = NSView()
cView.wantsLayer = true
cView.layer?.backgroundColor = NSColor.black.cgColor let dView = NSView()
dView.wantsLayer = true
dView.layer?.backgroundColor = NSColor.purple.cgColor view.addSubview([aView, bView, cView, dView],
constraints: [.height(80), .top(20)], spacing: (10, -10, 20), direction: .horizontal)
}
}

多个View等高垂直排列

import ElegantSnap

class ViewController: NSViewController {

    override func viewDidLoad() {
super.viewDidLoad() let aView = NSView()
aView.wantsLayer = true
aView.layer?.backgroundColor = NSColor.red.cgColor let bView = NSView()
bView.wantsLayer = true
bView.layer?.backgroundColor = NSColor.blue.cgColor let cView = NSView()
cView.wantsLayer = true
cView.layer?.backgroundColor = NSColor.black.cgColor let dView = NSView()
dView.wantsLayer = true
dView.layer?.backgroundColor = NSColor.purple.cgColor view.addSubview([aView, bView, cView, dView],
constraints: [.width(80), .leading(20)], spacing: (10, -10, 20), direction: .vertical)
}
}

前往主页

最新文章

  1. Please Call Me NIO
  2. hdu4924 Football Manager
  3. .NET Reactor 命令行使用
  4. 项目图片上传存储的目录部分代码思路Calendar类获取年月日
  5. erlang 里的if 和 case
  6. 计算机本科/硕士找offer的总结 节选
  7. How I Mathematician Wonder What You Are!(poj 3130)
  8. 多态,封装,反射,类内置attr属性,os操作复习
  9. CodeForces Round #550 Div.3
  10. MyEclipse 10.X激活方法
  11. Error: File 'C:\somepath\bin\ARM\Release\App_1.1.218.0_scale-100.appx' not found.
  12. Node.js WEB服务器(1)——编写简单的HTTP服务器
  13. installshield路径
  14. sql分组获取第一条或者最小一条值
  15. JavaScript-----截取字符串的常用方法
  16. wqCms6.0在IIS6的Getshell
  17. VMware 增加硬盘ubuntu
  18. Git 设置 SOCKS 代理
  19. HihoCoder1050 树中的最长路 树形DP第三题(找不到对象)
  20. iOS 9应用开发教程之ios9中实现button的响应

热门文章

  1. 吴裕雄--python学习笔记:爬虫
  2. ltp压力测试结果分析脚本
  3. 使用document.domain+iframe跨域实例
  4. Future 异步多线程
  5. 剑指CopyOnWriteArrayList
  6. 网购分期不还 N种恶果等着你
  7. 【Java】macOS下编译JDK8
  8. Simplifying Conditional Expressions(简化条件表达式)
  9. C++走向远洋——53(项目一1、分数类的重载、加减乘除、比较)
  10. java算法--链表