电路图 1.0.0

电路图 1.0.0

高尔顿‘拉希姆’维护。



电路图 1.0.0

电路图

CI Status Version License Platform

一个用于视图布局的声明式 Swift 微框架

之所以选择这个名字,是因为电气电路图展示了电路的连接。由于我们的名字来源于电力单位,所以我们觉得这个主题很合适。

动机

查看此文档,了解此框架是如何产生的解释。

总结来说,我希望从以下

NSLayoutConstraint.activate([
    a.leadingAnchor.constraint(equalTo: parentView.leadingAnchor, constant: 10.0),
    a.trailingAnchor.constraint(equalTo: parentView.trailingAnchor, constant: -10.0),
    a.topAnchor.constraint(equalTo: parentView.topAnchor, constant: 10.0),
    a.bottomAnchor.constraint(equalTo: parentView.bottomAnchor, constant: -10.0)
])

转换为以下类似的内容

a.applyLayout([
    .alignToEdges(of: parentView)
])

主要的变化是,现在我们不再使用组合模式并构建很多数组,而只有一个简单的类,它作为一个链表节点,以避免创建数组的开销。这使得约束的运用更加简单。

愿景

Wattpad iOS 开发者正积极在我们的应用程序中使用这个框架。

现在,该项目开源了,我们希望每个人都能为其添加更多的功能,以便在保持干净界面的同时使其更加强大。

用法

使用 Schematic 非常简单,最佳之处在于 translatesAutoresizingMaskIntoConstraints 会被自动设置为 false

Constraint 类有大量有用的扩展,您可以用它们来声明您的布局。例如,如果我们想要在父视图 parentView 中以固定大小居中视图 view

viewA.applyLayout([
    .center(in: parentView),
    .makeSize(equalTo: 20.0) // Infers that you want a 20x20 size
])

另一个例子,可以是将个人资料照片 profileImageView 与标签 usernameLabel 对齐到右方,同时在父视图 parentView 中垂直居中。

// Support laying out multiple views at once
[profileImageView, usernameLabel].applyLayout([
    .centerY(in: parentView)
])

profileImageView.applyLayout([
    .matchLeadingTo(parentView),
    .alignTrailing(to: .leading, of: usernameLabel, withOffset: -10.0)
])

usernameLabel.applyLayout([
    .matchTrailingTo(parentView)
])

要获取对约束(NSLayoutConstraint)的引用,您只需调用一次布局即可。

// Returns the first constraint that was used to create this layout
let constraint = usernameLabel.applyLayout(
    .matchTrailingTo(parentView)
)

// The first constraint rule is done because for multiple constraints, it would be troublesome to return all of them.
// Here, the constraint could be centerX or centerY so this is best used for single constraints.
let constraint = usernameLabel.applyLayout(
    .center(in: parentView)
)

示例

要运行示例项目,请克隆仓库,然后首先从示例目录运行 pod install

需求

安装

该图可以通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中。

pod 'Schematic'

作者

Alexander Figueroa,[email protected]

许可证

该图可在 MIT 许可下使用。更多信息请参阅 LICENSE 文件。