Constrictor 6.0.1

Constrictor 6.0.1

Pedro Carrasco 维护。



Constrict (/ˈkənˈstrɪkt/)动词

"... 使某物变得更紧、更窄:"

使用方便链式操作的 Constrictor 来约束您的 Auto Layout 代码。

Presentation

Build Status codecov CocoaPods Carthage compatible GitHub contributors apm

功能

  • 兼容 Auto Layout👍
  • 简洁且可链式语法⛓️
  • 自动设置 translateAutoresizingMaskIntoConstraints😍
  • 约束默认为活动状态 🤩
  • 轻松更新约束🏃
  • 允许在创建时设置优先级💁‍♂️

用法示例⌨️

链式操作

Constrictor 的一个关键特性是其易于链式操作。以下是一个示例

label.constrictor
    .size(view, multiplyBy: 0.75)
    .center(view)
    .bottom(as: .greaterOrEqual, to: imageView, .top)

简单

Constrictor 允许您完全替换 NSLayoutAnchor。例如

// NSLayoutAnchor
label.topAnchor.constraint(equalTo: view.topAnchor)

// Constrictor
label.constrictor.top(to: view)

另一个锚点和偏移量?这样做

// NSLayoutAnchor
label.topAnchor.constraint(equalTo: view.bottomAnchor, constant: 10)

// Constrictor
label.constrictor.top(to: view, .bottom, with: 10)

关系、优先级和不活动?没问题!

// NSLayoutAnchor
let constraint = label.topAnchor.constraint(greaterThanOrEqualTo: view.topAnchor)
constraint.priority = .defaultHigh

// Constrictor
label.constrictor.top(as: .greaterOrEqual, to: view, prioritizeAs: .high, is: .disabled)

Edge

如何使用 NSLayoutAnchor 约束边缘

label.topAnchor.constraint(equalTo: view.topAnchor)
label.bottomAnchor.constraint(equalTo: label.bottomAnchor.constraint)
label.leadingAnchor.constraint(equalTo: view.leadingAnchor)
label.trailingAnchor.constraint(equalTo: view.trailingAnchor)

使用 Constrictor 可以轻松实现

label.constrictor.edge(to: view)

想要改变前面和后面的间距吗?

label.constrictor.edge(to: view, with: .horizontal(15))

如果你只想约束除了底部以外的所有边缘怎么办?

label.constrictor.edge(to: view, .top, .leading, .trailing, with: .horizontal(15))

Center

使用 NSLayoutAnchor 进行居中

label.centerXAnchor.constraint(equalTo: view.centerXAnchor)
label.centerYAnchor.constraint(equalTo: view.centerXAnchor)

使用 Constrictor

label.constrictor.center(in: label)

不同的偏移量?

label.constrictor.center(in: label, with: .centerX(-16) & .centerY(32))

Size

使用 NSLayoutAnchor 定义大小

label.widthAnchor.constraint(equalToConstant: 10)
label.heightAnchor.constraint(equalTo: label.widthAnchor)

Constrictor 做得更好

label.constrictor.size(to: 10)

用乘数到另一个视图?就像这样

label.constrictor.size(view, multiplyBy: 0.75)

Animate

每个人都很喜欢动画,Constrictor 也不例外

// Only have one constraint for an anchor?
label.constrictor.updateFirst(.top) { $0?.enable() }

// Have two constraints for an anchor but for different elements? Provide more details
label.constrictor.update(.bottom, to: imageView) { $0?.constant = 16 }

// Call UIView.animate(...) { view.layoutIfNeeded() } to animate changes

Installation📦

CocoaPods

Constrictor 可以通过 CocoaPods 获取。为此,请将以下行添加到您的 PodFile 中

pod 'Constrictor'

然后在终端中运行以下命令

pod install

Carthage

将此内容添加到您的Cartfile中

github "pedrommcarrasco/Constrictor"

然后在终端中运行以下命令

carthage update

Sample Project📲

此仓库中有一个名为Example的示例项目,如果您想在项目中使用Constrictor之前先看看它,请随时查看它并尝试用它应用一些约束。

贡献🙌

通过报告错误或提交pull requests来向此项目贡献力量。此外,您也可以在我的Twitter上提出任何问题。

许可协议⛔

Constrictor遵循MIT许可协议。有关更多信息,请参阅LICENSE文件。