XAutoLayout 2.1.1

XAutoLayout 2.1.1

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

kaizeiyimi 维护。



  • kaizei

XAutoLayout

使用一些 Swift 的神奇功能,提供了一种非常简洁清晰的代码中制作约束的方式。

XCode 8 及以上

由于 Swift 3 是必需的,您需要 XCode 8 及以上。如果您需要 Swift 2.3 版本,请使用 1.3.0。

最新版本

当前最新版本是 2.1.1

快速浏览

您必须了解 'left.attr = right.attr * m + c' 对 AutoLayout 的含义。

import XAutoLayout

// use `=/`` to make composite equal witch creates an array of NSLayoutConstraint
NSLayoutConstraint.activateConstraints( v1.xEdge =/ [10,5,-10,-20] )

// use 'xmakeConstraints' to make constraints. constraints can be adjusted by 'direction'
// direction is default to 'LeadingToTrailing' and autoActive is default to 'true' 
xmakeConstraints(.RightToLeft, autoActive: true) {
    v2.xSize =/ [50, view.xHeight.xc(-80)]
    // since the direction is 'RightToLeft', the "v2.xLeading =/ 10" will be adjusted as "v2.xRight =/ -10"
    [v2.xTop, v2.xLeading] =/ [20, 10.xp(750)]
    // notice that in 'v2.xLeading =/ 10' the right var is a number 10, 
    // since no view is specified, superview of v2 will be used as second item, and secondAttr is same as firstAttr.
    // so the result is same as "v2.xLeading =/ v2.superview.xLeading.c(10)"
}

// and you should know "v.xHeight =/ 20" will not use superview, because height and width are dimension not anchor.

// we can also adjust constraint's constaint, multiplier and priority
v.xWidth =/ view.xWidth.xm(2).xc(-20).xp(750)

它易于阅读和编写。不是吗?

支持您的类型

  1. 首先,您需要确认实现 XFirstItem 协议并实现 xGenerate
func xGenerate() -> XAttribute {
    return XAttribute(item: yourItem, attr: yourAttr)
}

yourItemyourAttr 是约束的第一项和第一属性。

  1. 第二项请参考 SignedNumberType 和 UnsignedIntegerType 的实现。
public func xGenerateX() -> XAttributeX {
    return XAttributeX(item: nil, attr: .NotAnAttribute, constant: CGFloat(("\(self)" as NSString).doubleValue))
}

项和属性将是约束的第二项和第二属性。如果您需要,您可以提供常量、乘数和优先级。但您必须了解您所做的事情。

AutoLayout 的意义是什么?

API 对开发者来说不友好。太长且太复杂。希望 XAutoLayout 让我们的工作更轻松。