此仓库提供了一些方便创建约束的方法。
view.pin(.edges)
view.pin(.leading)
view.pin(.edges(.horizontal), 16)
view.pin(edges(.top, .bottom), 10, to: safeAreaLayoutGuide)
view.pin(edges: [.top: 10, .horizontal: 5], to: safeAreaLayoutGuide)
// pin view after/before another view
rightView.pin(to: .trailing, of: leftView, options: .offset(10))
view.pin(.center, 10)
view.pin([.size: 100, .centerY: 10, .top: 20, .edges(.horizontal): 10])
view.pin(.top, to: .centerY, of: anotherView)
view.pin(aspectRatio: 2 / 3)
with options 参数。view.pin(.edges, 10, options: .priority(.required))
选项
.priority(300): 设置约束的优先级.offset(10): 设置约束的偏移量.multiplier(2): 设置约束的乘数.relation(.greaterThanOrEqual), .greaterThanOrEqual, .lessThanOrEqual: 设置约束的关系.inactive, .activated(false): 设置约束的激活状态.update: 如果约束已存在,则更新约束.translatesAutoresizingMask: 不要更改 translatesAutoresizingMaskIntoConstraints 属性.safeArea: 使用 safeAreaLayoutGuide 代替 superview 作为默认的第二个项目view
.pin(.edges(.horizontal), 10)
.pin(to: .bottom, of: anotherView.pin(.height, 50), options: .offset(20))
.pin(.size, 100)
PartialRangeThrough 和 PartialRangeFrom 用于偏移量值view.pin(.bottom, 10..., with: .safeArea)
view.pin(to: .bottom, of: anotherView, options: .offset(...10))
Constraints 对象,可用于更新约束。创建一个 Package.swift 文件。
// swift-tools-version:5.7
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/VDPin.git", from: "1.7.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["VDPin"])
]
)
$ swift build
将以下行添加到您的 Podfile
pod 'VDPin'
并首先从 podfile 目录运行 pod update。
dankinsoid, voidilov@gmail.com
VDPin 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。