BEPureLayout 0.2.1

BEPureLayout 0.2.1

bigearsenal 维护。



BEPureLayout

CI Status Version License Platform

示例

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

要求

安装

BEPureLayout 可通过 CocoaPods 获取。要安装它,只需将以下行添加到 Podfile 中即可

pod 'BEPureLayout'

用法

BEPureLayout 添加了一些自定义 UI 类和简写辅助函数,这些函数针对知名的 PureLayout 库,帮助开发者快速创建常见的 UIComponents,比如纯 UIView, UIButton, UILabel, UIImageView 等。此外,该库还提供了一些常用的方法来添加约束。

BEViewController + BENavigationController

BEViewController 是 UIViewController 的一个子类,它实现了简便配置和设置导航栏和状态栏的方法。

  • BEViewController 应该嵌入到 BENavigationController 中。
  • 布局代码应该在 override func setUp() 中实现。
  • 数据绑定应该在 override func bind() 中实现。
  • 为了自定义导航栏,重写属性 var preferredNavigationBarStype: BEViewController.NavigationBarStyle
  • 为了改变导航栏的背景颜色,使用 setNavigationBarBackgroundColor(:)
  • 为了改变导航栏的文本样式,使用 setNavigationBarTitleStyle(textColor: UIColor, font: UIFont)
  • 为了改变状态栏样式,使用 changeStatusBarStyle(_ style: UIStatusBarStyle)

UIView

init(width:, height:, backgroundColor:, cornerRadius:)

UIButton

init(width:, height:, backgroundColor:, cornerRadius:, label:, labelFont:, textColor:, contentInsets:)

UILabel

init(text:, textSize:, weight:, textColor:, numberOfLines:, textAlignment:)

UIImageView

init(width:, height:, backgroundColor:, cornerRadius:, imageNamed:, contentMode:)

UITextView

init(width:, height:, backgroundColor:, cornerRadius:, font:, keyboardType:, placeholder:, autocorrectionType:, autocapitalizationType:, spellCheckingType:, textContentType:, isSecureTextEntry:, horizontalPadding:, leftView:, leftViewMode:, rightView:, rightViewMode:)

注意:您不需要为初始化器提供所有属性。仅提供已知或必需的属性,然后其他属性将默认设置。例如,如果视图的宽度取决于父视图的宽度,则只需从初始化器中删除宽度属性:UIView(height: 30),并将所需的宽度约束添加到父视图中

添加约束的新方法

  • autoPinToTopLeftCornerOfSuperview
  • autoPinToTopRightCornerOfSuperview
  • autoPinToBottomLeftCornerOfSuperview
  • autoPinToBottomRightCornerOfSuperview
  • autoPinToTopLeftCornerOfSuperviewSafeArea
  • autoPinToTopRightCornerOfSuperviewSafeArea
  • autoPinToBottomLeftCornerOfSuperviewSafeArea
  • autoPinToBottomRightCornerOfSuperviewSafeArea
  • autoPinBottomToSuperViewSafeAreaAvoidKeyboard

ContentHuggingScrollView

这个UIScrollView子类包含一个contentView,它被包含在ScrollView本身内部,并且只能在由属性scrollableAxis提供的定义方向中滚动。需要添加到ScrollView中的所有内容都应该只添加到其contentView中。例如

let scrollView = ContentHuggingScrollView(scrollableAxis: .vertical)
view.addSubview(scrollView)
scrollView.autoPinEdgesToSuperviewEdges()

let label = UILabel(text: "Very long text.....")

scrollView.contentView.addSubview(label)
label.autoPinEdgesToSuperviewEdges()

BETableHeaderView

这个UIView子类允许您为UITableView创建灵活的tableHeaderView。在BETableHeaderView中执行任何布局更改后,它将重新分配自己作为navigationBar的tableHeaderView,并在navigationBar中触发重新布局。

BETableHeaderView的使用很简单,您可以创建其子类并覆盖commonInit函数来布局其内容

class MyTableHeaderView: BETableHeaderView {
    lazy var label = UILabel(text: "TableViewHeaderView is loading...", textSize: 20, weight: .bold, textColor: .black, numberOfLines: 0)
    
    override func commonInit() {
        super.commonInit()
        
        addSubview(label)
        label.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets(top: 16, left: 16, bottom: 0, right: 16))
    }
}

然后,您可以使用初始化器在任何与UITableView一起使用的MyTableHeaderView进行构造

let tableHeaderView = MyTableHeaderView(tableView: tableView)

因此,可以自由更改headerView的内容,而无需关心重新布局

tableHeaderView.label.text =
    """
    TableHeaderView was loaded

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    """

有关更多详细信息,请参阅示例项目

作者

bigearsenal, [email&protected]

许可

BEPureLayout遵循MIT许可。更多信息请参见LICENSE文件。