AutoLayoutDSL-Swift 1.0.2

AutoLayoutDSL-Swift 1.0.2

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最后发布2017年9月
SwiftSwift 版本3.0
SPM支持 SPM

赵阳阳 维护。



  • 作者:
  • 赵阳阳

AutoLayoutDSL-Swift

AutoLayoutDSL-Swift 是一个简单的 swift DSL/扩展,用于更方便的自动布局管理

功能

  • [x] 对创建 NSAutoLayoutConstraint 进行数学运算:+,-,*,/,==,>=,<=
  • [x] 便利的中缀运算 =>,~~>,~~~> 以链模式添加约束

要求

  • iOS 7.0+
  • Xcode 6.3

交流

  • 如果您需要帮助,请使用 Stack Overflow
  • 如果您想询问一个一般性的问题,请使用 Stack Overflow
  • 如果您发现了错误,请提交问题。
  • 如果您有功能请求,请提交问题。
  • 如果您想贡献,提交拉取请求。

安装

**内嵌框架需要最低部署目标为 iOS 8**

要使用针对 iOS 7 的项目中的 AutoLayoutDSL-Swift,您必须直接将位于 源目录 中所有 Swift 文件包含到您的项目中。有关其他说明,请参阅 ‘源文件’ 部分。


使用

创建 NSLayoutConstraints

import AutoLayoutDSL-Swift

view.left == parentView.left + 10
// is equal to 
NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal, toItem: parentView, attribute: .Left, multiplier: 1, constant:10


view.right >= parentView.right - 10
// is equal to 
NSLayoutConstraint(item: view, attribute: .Right, relatedBy: .GreaterThanOrEqual, toItem: parentView, attribute: .Right, multiplier: 1, constant:- 10

view.height == anotherView.height * 2
// is equal to 
NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal, toItem: anotherView, attribute: .Height, multiplier: 2, constant:0

view.height == anotherView.height / 2
// is equal to 
NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal, toItem: anotherView, attribute: .Height, multiplier: 0.5, constant:0


view.height == 100
// is equal to 
NSLayoutConstraint(item: view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant:100

添加 NSLayoutConstraints

// 4 NSLayoutConstraints were added and all their priority will be UILayoutPriorityRequired
self.view => self.redView.left == self.view.left + 10
        => self.redView.right == self.view.right - 10
        => self.redView.top == self.view.top + 30
        => self.redView.height == 100

// 4 NSLayoutConstraints were added 
// First two constraints' priorities will be UILayoutPriorityRequired
// Third constraint‘s priority will be UILayoutPriorityDefaultHigh
// Fourth constraint's priority will be UILayoutPriorityDefaultLow
self.view => self.redView.left == self.view.left + 10
        => self.redView.right == self.view.right - 10
        ~~> self.redView.top == self.view.top + 30
        ~~~> self.redView.height == 100

// If you want custom priority, you can write as belowing
var constaint = self.redView.left == self.view.left + 10
constraint.priority = UILayoutPriorityDefaultHigh - 1
self.view => constraint // => doesn't change priority


常见问题解答 (FAQ)

我应该何时使用 AutoLayoutDSL-Swift?

如果您正在使用 Swift 编写 iOS 项目,并且您希望通过手动编写代码来创建 UI 而不是使用 Storyboard,但您仍然想利用 AutoLayout 的优势来节省框计算,并使用自动处理动态内容,AutoLayoutDSL-Swift 可以是您的一个选择,这样您就不需要编写很多代码来创建 NSLayoutConstraint 实例,还可以使代码更加简洁和直接。


鸣谢

AutoLayoutDSL-Swift受到AutoLayoutDSL的启发,然而AutoLayoutDSL是用Objective C++编写的。

许可证

AutoLayoutDSL-Swift采用MIT许可证发布。详细信息请参阅LICENSE文件。