1 Star 0 Fork 0

Harvey520 / ElegantSnap

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ElegantSnap

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

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

Platform Cocoapods Compatible Carthage compatible

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)])
        
        // OR Chain 链式使用
        // v1.5.0+
        // view.addSubview(aView) { $0.top().leading().width(200).height(400).end() }
        
        // view.addSubview(aView)
        // aView.make([.top(), .leading(), .width(200), .height(400)])
         
        // OR Chain 链式使用
        // v1.5.0+
         view.make { $0.top().leading().width(200).height(400).end() }
    }
}

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)])
        
        // OR Chain 链式使用
        // v1.5.0+
        // view.addSubview(aView) { $0.top().leading().width(200).height(400).end() }
        
        let myView = NSView()
        view.addSubview(myView, constraints: [.top(aView.snp.bottom, 20), .leading(), .width(300), .height(aView.snp.height)])
        
        // OR Chain 链式使用
        // v1.5.0+
        // view.addSubview(myView) { $0.top(aView.snp.bottom, 20).leading().width(300).height(aView.snp.height).end() }
    }
    
}

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

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

vertical

License / 许可证

ElegantSnap is released under the MIT license. See LICENSE for details.

ElegantSnap 是在 MIT 许可下发布的,有关详情请查看该许可证。

Copyright (c) 2019-Present ElegantSnap Team - https://github.com/ZuopanYao/ElegantSnap Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
Swift 等 4 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/harvey520/ElegantSnap.git
git@gitee.com:harvey520/ElegantSnap.git
harvey520
ElegantSnap
ElegantSnap
master

搜索帮助