AMGAutolayout 1.0.1

AMGAutolayout 1.0.1

Albert Montserrat 维护。



  • AlbertMontserrat

AMGAutolayout

CI Status Version License Platform

工作原理

以下是一些使用 AMGAutolayout 可以完成的示例

//With operators

//Directly from layout property (this doesn't make `translatesAutoresizingMaskIntoConstraints = false`)
secondTitleLabel.layout.top == textLabel.layout.bottom + 50

//Or with makeLayout which makes `translatesAutoresizingMaskIntoConstraints = false` to the current view
secondTitleLabel.makeLayout {
    $0.left == titleLabel.layout.left
    $0.right == titleLabel.layout.right - 30
    $0.height == 50
}

secondTextLabel.makeLayout {
    topConstraint2 = $0.top == secondTitleLabel.layout.bottom + 20
    $0.centerX <= secondTitleLabel.layout.centerX
    $0.width == secondTitleLabel.layout.width - 50
    $0.height == secondTitleLabel.layout.height * 3 + 50
}

frameView.makeLayout {
    $0.top >= secondTextLabel.layout.bottom + 20
    $0.left == backgroundView.layout.left + 20
    $0.right <= backgroundView.layout.right - 20
    $0.aspectRatio(5)
    $0.bottom == backgroundView.layout.bottom - 20
}

frameView2.makeLayout {
    $0.centerSuperview()
    $0.width <= 50
    $0.height >= 20
}

frameView3.makeLayout {
    $0.center(frameView2)
    $0.width == 50
    $0.height == 20
}

backgroundView.makeLayout {
    $0.fillSuperview(with: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20), relativeToSafeArea: true)
}

//Without operators

titleLabel.makeLayout {
    topConstraint = $0.top.equal(to: backgroundView.layout.top, offsetBy: 20)
    $0.left.equal(to: backgroundView.layout.left, offsetBy: 20)
    $0.right.equal(to: backgroundView.layout.right, offsetBy: -20)
    $0.height.equal(to: 52)
}

textLabel.makeLayout {
    $0.top.equal(to: titleLabel.layout.bottom, offsetBy: 20)
    $0.centerX.equal(to: titleLabel.layout.centerX)
    $0.width.equal(to: titleLabel.layout.width, constant: 10)
    $0.height.equal(to: titleLabel.layout.height, multiplier: 2, constant: 10)
}

view.layout 变量具有以下属性

public lazy var leading = property(with: view.leadingAnchor)
public lazy var trailing = property(with: view.trailingAnchor)
public lazy var left = property(with: view.leftAnchor)
public lazy var right = property(with: view.rightAnchor)
public lazy var top = property(with: view.topAnchor)
public lazy var bottom = property(with: view.bottomAnchor)
public lazy var width = dimensionProperty(with: view.widthAnchor)
public lazy var height = dimensionProperty(with: view.heightAnchor)
public lazy var centerX = property(with: view.centerXAnchor)
public lazy var centerY = property(with: view.centerYAnchor)

//Safe area
public lazy var safeTop = property(with: view.safeAnchorContainer.topAnchor)
public lazy var safeBottom = property(with: view.safeAnchorContainer.bottomAnchor)
public lazy var safeLeft = property(with: view.safeAnchorContainer.leftAnchor)
public lazy var safeRight = property(with: view.safeAnchorContainer.rightAnchor)
public lazy var safeLeading = property(with: view.safeAnchorContainer.leadingAnchor)
public lazy var safeTrailing = property(with: view.safeAnchorContainer.trailingAnchor)
public lazy var safeWidth = dimensionProperty(with: view.safeAnchorContainer.widthAnchor)
public lazy var safeHeight = dimensionProperty(with: view.safeAnchorContainer.heightAnchor)
public lazy var safeCenterX = property(with: view.safeAnchorContainer.centerXAnchor)
public lazy var safeCenterY = property(with: view.safeAnchorContainer.centerYAnchor)

所有这些属性都是可用的

var leadingAnchor: NSLayoutXAxisAnchor { get }
var trailingAnchor: NSLayoutXAxisAnchor { get }
var leftAnchor: NSLayoutXAxisAnchor { get }
var rightAnchor: NSLayoutXAxisAnchor { get }
var topAnchor: NSLayoutYAxisAnchor { get }
var bottomAnchor: NSLayoutYAxisAnchor { get }
var widthAnchor: NSLayoutDimension { get }
var heightAnchor: NSLayoutDimension { get }
var centerXAnchor: NSLayoutXAxisAnchor { get }
var centerYAnchor: NSLayoutYAxisAnchor { get }

可以使用的运算符有

  • property1 == property2

  • property1 >= property2

  • property1 <= property2

  • dimensionProperty1 == dimensionProperty2

  • dimensionProperty1 >= dimensionProperty2

  • dimensionProperty1 <= dimensionProperty2

  • dimensionProperty1 == constant

  • dimensionProperty1 >= constant

  • dimensionProperty1 <= constant

对于 property2 的部分,您还可以这样做

  • property2 + constant
  • property2 - constant

对于 dimensionProperty2 的部分

  • dimensionProperty2 * multiplier
  • dimensionProperty2 * multiplier + constant
  • dimensionProperty2 * multiplier - constant

要求

iOS 9.0 是必需的

安装

AMGAutolayout可以通过CocoaPods获得。要安装它,只需在您的Podfile中添加以下行

pod 'AMGAutolayout'

作者

AlbertMontserrat, [email protected]

这个库遵循并扩展了John Sundell在这篇帖子中创建的定制DSL

https://www.swiftbysundell.com/posts/building-dsls-in-swift

在这个库中有很多新增功能,例如

  • 直接与尺寸常量协同工作,如$0.height == 30
  • 直接与属性协同工作,并完全摆脱view.xyzAnchor
  • 添加了很多扩展,例如,简单地将安全区域设置为$0.top = $1.layout.safeTop
  • 允许使用自定义运算符,甚至可以将比例因子与偏移量组合以执行类似$0.height = $1.layout.height * 2 + 10的操作
  • 返回生成的约束,甚至可以用操作符函数处理视控制器中的后续更改。还添加了@discardableResult以避免常规使用中的警告
  • 还有更多...

许可协议

AMGAutolayout在MIT许可下可用。有关更多信息,请参阅LICENSE文件。