PrettyConstraints
PrettyConstraints 帮助您使用 AutoLayout 约束更快地以编程方式创建视图。
安装
CocoaPods
使用 CocoaPods,通过向您的 Podfile 添加以下行进行安装
pod 'PrettyConstraints'
Swift Package Manager
使用 Swift Package Manager,在 Xcode 11.0 或更高版本中将它作为 Swift 包添加,在 Xcode 中选择“文件 > Swift 包 > 添加包依赖...”并添加存储库 URL
https://github.com/mobiraft/PrettyConstraints
手动安装
下载并将《PrettyConstraints》文件夹和文件包含到您的代码库中。
要求
- iOS 11
- Swift 5
如何使用
在另一个视图中嵌入视图
let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 100)))
view.backgroundColor = .black
let subView = UIView()
subView.backgroundColor = .gray
view.addSubview(subView)
subView.applyConstraints(.fitInView(view, inset: 10))
在另一个视图中居中视图
subView.applyConstraints(.center(in: view),
.width(constant: 50),
.height(constant: 50))
其他约束示例
subView.applyConstraints(.top(to: view.topAnchor),
.bottom(to: view.bottomAnchor),
.leading(to: view.leadingAnchor, equality: .greaterThanOrEqual),
.trailing(to: view.trailingAnchor, equality: .lessThanOrEqual),
.centerX(to: view.centerXAnchor),
.width(constant: 50),
.height(constant: 50))
存储约束并在需要时修改
let constraints:[NSLayoutConstraint] = subView.applyConstraints(.center(in: view),
.width(constant: 50),
.height(constant: 50))
constraints[1].constant = 20 // Modifies the width as 20.0
博客
- 使用 PrettyConstraints 在没有 Storyboards 的情况下创建应用 | Mobiraft
- 没有 Storyboards 的 iOS 应用使用 PrettyConstraints | 作者:Hardik Parmar | 2020 年 11 月 | Medium
联系我们
网站
访问我们获取更多有关 Android、SwiftUI 和 Swift 的文章。 www.mobiraft.com