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

ElegantSnap

https://github.com/ZuopanYao/ElegantSnap

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

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

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 ElegantSnap into your project manually.

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

Usage / 使用

V1.5.0+ 支持 Chain 链式调用

let aView = UIView()
let bView = UIView()
let cView = UIView()

view.addSubview(cView)
cView.make { $0.top().leading().width(88).height(388).end() }

view.addSubview(aView) { $0.top().leading().width(200).height(400).end() }
view.addSubview(bView) {
	$0.top(200)
  		.leading(aView.snp.leading)
        .height(aView.snp.height)
        .width(100)
        .end()
}

Compare with SnapKit / 与 SnapKit 比较

ElegantSnapSnapKit
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)
    }
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Harvey66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值