工具箱 (Toolbox) 是一个 iOS 开发框架,包含从一个项目到另一个项目重复复制的代码片段和扩展。 使用工具箱的目的是节省时间并摆脱复制/粘贴的操作。
使用 import ToolBox
将框架导入到你的 class
中。
现在你可以使用一些辅助工具,例如:
旧方法
let fooView: UIView() = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .white
return view
}()
新方法
let fooView = UIView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .white
}
如果字符串是可选的 (optional)、nil、空字符串、空空格、制表符 (tab)、换行符 (newline) 或回车符 (return),它将返回 true。
let string: String?
string.isBlank // => true
let string: String? = nil
string.isBlank // => true
let string = ""
string.isBlank // => true
let string = " "
string.isBlank // => true
let string = "\n"
string.isBlank // => true
告别 Index out of range
(索引超出范围)
let array = [1, 2, 3, 4]
array[safeIndex: 6] => nil
添加 UIView 的子视图 (subView)
// old
view.addSubview(foo)
view.addSubview(bar)
// new
view.add(foo, bar)
添加 UIStackView 的 arrangedSubview
// old
stackView.addArrangedSubview(foo)
stackView.addArrangedSubview(bar)
// new
stackView.addArrangedSubviews(foo, bar)
现在更容易地删除 stackView 中的所有子视图 stackView.removeAllArrangedSubviews()
let separatorView = UIView.createSeparatorView(width: UIScreen.main.bounds.width, height: 1.0, color: .gray, priority: .required)
摆脱使用旧方法定义 UILabel 或 UIButton 的样式
旧方法不一致,如果您需要更改字体大小,则需要全部浏览并手动更改。
let label = UILabel().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.font = UIFont(name: "HelveticaNeue-UltraLight", size: 20.0)
$0.textColor = .darkText
}
使用样式的新方法
// Define the styles on one place e.g. Appearance.swift
extension UIColor {
struct App {
static let text = .darkText
}
}
extension Style {
static let headline1 = Style(font: UIFont(name: "HelveticaNeue-UltraLight", size: 20) ?? .systemFont(ofSize: 20), color: UIColor.App.text)
}
// and now in your classes you can use it like this
let label = UILabel().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.setStyle(.headline1)
}
当被管理的 UIViewControllers 被解除 (dismiss) 时,协调器 (Coordinators) 使用 UIAdaptivePresentationControllerDelegate
来接收回调。 仅当协调器以模态 (modally) 方式呈现时(通过 present(_ coordinator: Coordinator, animated: Bool, completion: (() -> Void)? = nil)
),委托 (delegate) 才会设置为协调器。 如果委托已经设置,则会抛出 fatalError,因为这是一个编程错误。
如果需要 UIAdaptivePresentationControllerDelegate
回调,可以直接在协调器本身中实现。 可以通过 targetAdaptiveDelegate
将委托回调转发到另一个对象。 默认情况下,targetAdaptiveDelegate
是协调器的 rootViewController
。 对于 NavigationCoordinator
,topViewController
是 targetAdaptiveDelegate
。
当推送 (push) NavigationCoordinator
时(例如,调用 push(_ coordinator: NavigationCoordinator, animated: Bool)
函数),会传递 UIAdaptivePresentationControllerDelegate
。
如果您没有收到回调,请检查
UIAdaptivePresentationControllerDelegate
targetAdaptiveDelegate
是否设置为正确的对象NavigationCoordinator
并拦截了回调将以下依赖项添加到您的 Package.swift
文件
.package(url: "https://github.com/allaboutapps/Toolbox", from: "1.0.0")
在任何你想使用 Toolbox 的文件中,不要忘记使用 import ToolBox
导入框架。