DeclarativeLayout 0.8.0

DeclarativeLayout 0.8.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2019年4月
SPM支持 SPM

Scott Robbins 维护。



  • 作者
  • HotCocoaTouch

Declarative Layout Banner

Version License Platform

Declarative Layout

一种声明性、直观且高效的视图布局方式。


摘要
声明性 告诉框架你视图的布局应该如何,并由框架智能地添加/修改/删除约束和视图。
直观 让你的代码能够从视觉上表达视图的层次结构。
灵活 使用你喜欢的任何自动布局约束 DSL,按照你通常的做法编写相同的约束。
小巧 小巧且易于阅读的 Swift 5 代码库。

用法 | 更新到新布局 | 安装 | 要求 | 基于 DeclarativeLayout 构建 | 许可证


用法

在需要布局视图的文件的顶部导入 Declarative Layout

import DeclarativeLayout

您将通过 ViewLayout 对象定义和更新布局,因此您首先要创建一个对象并将其存储在一个属性中。

确保它在视图加载后进行初始化。例如,可以在 viewDidLoad 中进行,或者考虑延迟初始化属性。

viewLayout = ViewLayout(view: view)

告诉 ViewLayout 您希望更新它

viewLayout.updateLayoutTo { (com) in
    ...
}

使用布局组件来定义新的视图,这些视图应在层次结构中,以及应激活的约束(注意:传递给 .constraints(_:) 方法的所有约束不应处于活动状态)。

以下是一个示例

viewLayout.updateLayoutTo { (com) in
    com.stackView(self.stackView) { (com) in
        com.constraints(
            com.ownedView.leadingAnchor.constraint(equalTo: com.superview.leadingAnchor),
            com.ownedView.trailingAnchor.constraint(equalTo: com.superview.trailingAnchor),
            com.ownedView.topAnchor.constraint(equalTo: com.superview.safeAreaLayoutGuide.topAnchor,
                                                constant: 35)
        )

        com.ownedView.axis = .vertical
        com.arrangedView(self.redView) { (com) in
            com.constraints(
                com.ownedView.heightAnchor.constraint(equalToConstant: 50)
            )
        }
        com.space(20)
        com.arrangedView(self.orangeView) { (com) in
            com.constraints(
                com.ownedView.heightAnchor.constraint(equalToConstant: 50)
            )
        }
        com.space(20)
        com.arrangedView(self.yellowView) { (com) in
            com.constraints(
                com.ownedView.heightAnchor.constraint(equalToConstant: 50)
            )
        }
        com.space(20)
        com.arrangedView(self.greenView) { (com) in
            com.constraints(
                com.ownedView.heightAnchor.constraint(equalToConstant: 50)
            )
        }
        com.space(20)
        com.arrangedView(self.blueView) { (com) in
            com.constraints(
                com.ownedView.heightAnchor.constraint(equalToConstant: 50)
            )
        }
        com.space(20)
        com.arrangedView(self.purpleView) { (com) in
            com.constraints(
                com.ownedView.heightAnchor.constraint(equalToConstant: 50)
            )
        }
    }
}

这将为您提供一个这样的视图

切换到新的布局

如果您想更新到新的布局,只需再次调用 updateLayoutTo 方法,定义您的布局应该是什么样式。框架将负责添加、移除和移动视图,以及激活、停用和修改约束。

例如,让我们随机排列这些视图,它们之间的间距和高度,每隔几秒钟以这种方式重新更新布局。结果看起来可能会是这样

let views = [redView,
             orangeView,
             yellowView,
             greenView,
             blueView,
             purpleView]

viewLayout.updateLayoutTo { (com) in
    com.stackView(self.stackView) { (com) in
        com.constraints(
            com.ownedView.leadingAnchor.constraint(equalTo: com.superview.leadingAnchor),
            com.ownedView.trailingAnchor.constraint(equalTo: com.superview.trailingAnchor),
            com.ownedView.topAnchor.constraint(equalTo: com.superview.safeAreaLayoutGuide.topAnchor,
                                                constant: 35),
        )

        com.ownedView.axis = .vertical
        for view in views.shuffled() {
            com.arrangedView(view) { (com) in
                let random = CGFloat(Int.random(in: 20..<100))
                com.constraints(
                    com.ownedView.heightAnchor.constraint(equalToConstant: random)
                )
            }
            com.space(CGFloat(Int.random(in: 0..<50)))
        }
    }
}

安装

DeclarativeLayout 可通过 CocoaPods 获取。要安装,请将以下行添加到您的 Podfile。

pod "DeclarativeLayout"

要求

  • iOS 9.0 或更高版本
  • 支持 Swift 5

建立在 DeclarativeLayout 之上构建

每个方法(例如,view(:))都会返回一个新的布局组件。使用传递给可空闭包的组件是可选的。这样做是为了简化在 DeclarativeLayout 之上构建更具观点的布局框架,并利用其约束和视图层次结构差异。

许可证

DeclarativeLayout 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。