WHC_Layout 1.1.6.2

WHC_Layout 1.1.6.2

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

netyouli 维护。



WHC_Layout 1.1.6.2

  • 吴海超(WHC)

WHC_Layout (Swift)


Build Status Pod Version Platform Pod License

  • 当前在 iOS 和 Mac OS X 平台上使用的最快、最简单的 UI 布局自动开源库,具有强大的动态布局约束处理能力
  • 提供更新约束的服务,方便快捷的动态 UI 布局。

Objective-C 版本 👉 WHC_AutoLayout

布局 api 内部自动支持兼容处理 iPhone X 头部齐刘海和底部黑线

添加 UILayoutGuide,safeAreaLayoutGuide 约束支持

重构布局核心升级,基于二叉树层遍历算法搜索约束主视图,对于自动处理跨视图层复杂约束关系更加健壮从容

封装集成抗拉伸和抗压缩 api 支持链式调用

介绍

  • 采用链式布局 api 调用方便
  • 包含一行代码即可计算 UITableViewCell 高度模块化
  • 包含 WHC_StackView 模块(UIStackView 替代系统)
  • 自动识别相同类型冲突并更新新约束
  • 支持更改约束优先级
  • 支持删除约束
  • 支持 iOS 和 Mac OS X
  • 自动覆盖和修改与类型约束的冲突

要求

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+
  • Xcode 8.0 或更高版本
  • Swift 5.1
  • 如果您想使用 swift3.2,请 pod WHC_Layout '~> 1.0.9'

备注

  • 当视图调用 removeFromSuperview () 时,视图必须调用 whc_ResetConstraints () 以清除缓存约束
  • Swift5 + 已经废弃了 initialize 类方法,如果需要使用自动计算 UITableViewCell 高度,可以在 AppDelegate 初始化方法中调用 UITableview.whc_initconfig ()

安装

  • CocoaPods: pod 'WHC_Layout'

用法

UILayoutGuide,safeAreaLayoutGuide

let guide = UILayoutGuide()
let view = UIView()

guide.whc_Left(10)
.whc_Top(0, toView: self.view.safeAreaLayoutGuide)
.whc_Right(10)
.whc_Height(30)

view.whc_Left(10)
.whc_Right(10)
.whc_Top(0, toView: guide)
.whc_Height(50)

自动高度视图

view.whc_Left(10)
    .whc_Top(10)
    .whc_Right(10)
    .whc_HeightAuto()

使用 lessEqual 或 greaterEqual (width <= 100 && width >= 20)

view.whc_Width(100).whc_LessOrEqual()
    .whc_Width(20).whc_GreaterOrEqual()

SnapKit/Masonry 更新约束的方式不友好

view.snp.updateConstraints {(make) -> Void in
    make.top.equalTo(superview.snp_top).with.offset(10) 
    make.left.equalTo(superview.snp_left).with.offset(20)
    make.bottom.equalTo(superview.snp_bottom).with.offset(-10)
    make.right.equalTo(superview.snp_right).with.offset(-10)
}

更新视图约束

从20个其他视图向左修改视图

view.whc_Left(20)
// or
view.whc_Left(20, toView: otherView)

可以直接在Xib和Storyboard中修改约束

如果xib视图目前已经修改为左边的约束

/// First remove the xib view of leading and then add new constraints
view.whc_RemoveAttrs(.leading)
    .whc_Left(10)

移除约束

移除与视图左相关的所有约束

view.whc_RemoveAttrs(.left)

要移除与视图关联的多个约束

view.whc_RemoveAttrs(.left, .leading, .top)
// or 
view.whc_RemoveTo(linkView, attrs: .left ...)

修改视图约束优先级

修改视图约束的低优先级为右边

view.whc_Right(10)
    .whc_PriorityLow()
// The higher the priority, the less likely to be stretched
// 设置抗拉伸优先级,优先级越高越不容易被拉伸
label.whc_ContentHuggingPriority(.defaultLow, for: .horizontal)

// 设置抗压缩优先级,优先级越高越不容易被压缩
// Compression priority, the higher the priority the less easy to be compressed
label.whc_ContentCompressionResistancePriority(.defaultLow, for: .horizontal)

一行代码计算单元格高度

没有重用方法计算单元格高度

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewCell.whc_CellHeightForIndexPath(indexPath, tableView: tableView)
}

重用方法计算单元格高度

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewCell.whc_CellHeightForIndexPath(indexPath, 
                                    tableView: tableView, 
                                    identifier: "kFirendsCircleCellIdentifier",
                                    layoutBlock: { (cell) in
        /// use model layout cell
        cell.setFriendModel(model)
    })
}

使用 WHC_StackView

创建 WHC_StackView

let stackView = WHC_StackView()
self.view.addSubview(stackView)

添加约束

stackView.whc_Left(10)
         .whc_Top(10)
         .whc_Right(10)
         .whc_HeightAuto()

配置 stackView

1. 设置填充

stackView.whc_Edge = UIEdgeInsetsMake(10, 10, 10, 10) // 内边距

2. 设置布局方向

stackView.whc_Orientation = .Vertical                  // 自动垂直布局

3. 设置子视图的水平间距

stackView.whc_HSpace = 10                             // 子视图横向间隙

4. 设置子视图的垂直间距

stackView.whc_VSpace = 10                             // 子视图垂直间隙

5. 添加子视图并开始布局

for _ in 0 ... 3 {
    let view = UIView()
    stackView.addSubview(view)        
}
stackView.whc_StartLayout()

提示

为了更多地自动UI布局,WHC_StackView组件,一行代码计算单元格高度的模块,请下载此示例检查具体用法。

许可协议

所有源代码均获得MIT许可证授权。