VanillaConstraints 1.0.4

VanillaConstraints 1.0.4

Joan Disho 维护。



  • 作者
  • Joan Disho

Twitter: @_disho

VanillaConstraints 是用 Swift 编写的 iOS 的简化且可链式使用的 AutoLayout NSLayoutConstraints

  • 🌯围绕 NSLayoutConstraints 的薄包装。
  • 🍬创建布局约束的简短语法。
  • 描述 NSLayoutConstraint 的链式方法。
  • 🔥使用 KeyPaths
  • 约束默认是有效的。
  • 🧘‍♂️不需要设置 translatesAutoresizingMaskIntoConstraints = false
  • 🤙没有外部依赖。

🛠安装

CocoaPods

要使用 CocoaPods 将 VanillaConstraints 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

pod 'VanillaConstraints'

然后,运行以下命令

$ pod install

Carthage

github "jdisho/VanillaConstraints" ~> 1.0

然后,运行以下命令

$ carthage update

手动安装

如果您不希望使用任何依赖管理器,您可以通过下载源代码并将文件放在您的项目目录中来手动将 VanillaConstraints 集成到您的项目中。

👨🏻‍💻 使用方法

tl;dr

它可以让您替换这个

anotherView.addSubview(view)

view.translatesAutoresizingMaskIntoConstraints = false

let top = view.topAnchor.constraint(equalTo: anotherView.topAnchor, constant: 16.0)
top.priority = .defaultLow
top.isActive = true

let trailing = view.trailingAnchor.constraint(lessThanOrEqualTo: anotherView.trailingAnchor)
trailing.priority = .defaultHigh
trailing.isActive = true

let bottom = view.bottomAnchor.constraint(equalTo: anotherView.bottomAnchor, constant: 16.0)
bottom.priority = .required
bottom.isActive = true

let leading = view.leadingAnchor.constraint(greaterThanOrEqualTo: anotherView.leadingAnchor, constant: 8.0)
leading.isActive = true

用这个替换💁‍♂️:

view.add(to: anotherView)
  .top(to: \.topAnchor, constant: 16.0, priority: .defaultLow)
  .trailing(to: \.trailingAnchor, relation: .equalOrLess, priority: .defaultHigh)
  .bottom(to: \.bottomAnchor, constant: 16.0)
  .leading(to: \.leadingAnchor, relation: .equalOrGreater, constant: 8.0)

⚠️如果未指定锚点的视图,它将受到添加位置的约束。

边缘 🤲

将视图固定在其他视图的边缘

anotherView.addSubview(view)

view.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
    view.topAnchor.constraint(equalTo: anotherView.topAnchor),
    view.leadingAnchor.constraint(equalTo: anotherView.leadingAnchor),
    view.bottomAnchor.constraint(equalTo: anotherView.bottomAnchor),
    view.trailingAnchor.constraint(equalTo: anotherView.trailingAnchor)
])

使用VanillaConstraints

view.add(to: anotherView).pinToEdges()

或者使用等间距

view.add(to: anotherView).pinToEdges(withMargins: 16.0)

或固定在不同于添加位置的其他视图上

view.add(to: anotherView).pinToEdges(of: someOtherView)

或固定在安全区域布局指南边缘

view.add(to: anotherView).pinToEdges(safeConstrainable: true) // false by default

中心🖖

将视图中心与其他视图对齐

anotherView.addSubview(view)

view.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
    view.centerXAnchor.constraint(equalTo: anotherView.centerXAnchor)
    view.centerYAnchor.constraint(equalTo: anotherView.centerYAnchor)
])

使用VanillaConstraints

view.add(to: anotherView).center()

或在不同位置的其他视图中心对齐

view.add(to: anotherView).center(in: someOtherView)

大小👋

设置视图的大小

anotherView.addSubview(view)

view.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
    view.widthAnchor.constraint(equalToConstant: 50.0)
    view.heightAnchor.constraint(equalToConstant: 50.0)
])

使用VanillaConstraints

view.add(to: anotherView).size(CGSize(width: 50.0, height: 50))

或使用其他关系

view.add(to: anotherView).size(CGSize(width: 50.0, height: 50), relation: .equalOrLess) // .equal by default 

支持的属性🚚

  • 顶部
  • 底部
  • 左边
  • 右边
  • 行进行
  • 行尾
  • 水平中心
  • 垂直中心
  • 宽度
  • 高度

🐨作者

这个小巧的库是用❤️Joan Disho 编写

📃许可证

VanillaConstraints 采用MIT许可证发布。有关更多信息,请参阅License.md