WHC_AutoLayoutKit 2.8.6

WHC_AutoLayoutKit 2.8.6

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2020年5月

netyouli维护。



  • 作者:
  • 吴海超(WHC)

WHC_AutoLayout


Build Status Platform Pod Version Pod License

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

Swift4.+ 版本 👉 WHC_Layout

修改 iOS11 安全布局机制,增加 whc_IsSafe 控制API 布局API内部自动支持兼容处理 iPhone X 头部齐刘海和底部黑线

添加 UCLASizeGuide,安全区域布局指南约束支持

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

添加抗拉伸和抗压缩API支持链式调用

介绍

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

要求

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+
  • Xcode 8.0 或更高版本

注解

  • 在视图调用 removeFromSuperview 方法时,视图必须调用 whc_ResetConstraints 方法来清除缓存约束

安装

  • CocoaPods: pod 'WHC_AutoLayoutKit'

使用说明

UILayoutGuide,safeAreaLayoutGuide

UILayoutGuide * guide = UILayoutGuide.new;
UIView * view = UIView.new;

guide.whc_LeftSpace(10)
.whc_TopSpaceToView(0, self.view.safeAreaLayoutGuide)
.whc_RightSpace(10)
.whc_Height(30);

view.whc_LeftSpace(10)
.whc_RightSpace(10)
.whc_TopSpaceToView(0, guide)
.whc_Height(50);

自动高度视图

view.whc_LeftSpace(10)
    .whc_TopSpace(10)
    .whc_RightSpace(10)
    .whc_HeightAuto();

使用小于等于或大于等于宽度(width <= 100 && width >= 20)

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

砖石布局/SnapKit 更新约束方式不友好

[view mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview.mas_top).with.offset(10); 
    make.left.equalTo(superview.mas_left).with.offset(20);
    make.bottom.equalTo(superview.mas_bottom).with.offset(-10);
    make.right.equalTo(superview.mas_right).with.offset(-10);
}];

更新视图约束

将视图左侧移动到20个其他视图

view.whc_LeftSpace(20);
// or
view.whc_LeftSpaceToView(20,otherView);

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

如果现在将 xib 的视图左侧约束进行了修改

/// First remove the xib view of leading and then add new constraints
view.whc_RemoveLayoutAttrs(NSLayoutAttributeLeading)
    .whc_LeftSpace(10);

移除约束

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

view.whc_RemoveLayoutAttrs(NSLayoutAttributeLeft);

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

view.whc_RemoveLayoutAttrs(NSLayoutAttributeLeft,NSLayoutAttributeLeading,NSLayoutAttributeTop);
// or 
view.whc_RemoveTo(linkView, attrs: NSLayoutAttributeLeft ...);

修改视图约束优先级

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

view.whc_RightSpace(10)
    .whc_PriorityLow();
// The higher the priority, the less likely to be stretched
// 设置抗拉伸优先级,优先级越高越不容易被拉伸
label.whc_ContentHuggingPriority(UILayoutPriorityDefaultLow, UILayoutConstraintAxisHorizontal);

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

一行代码计算单元格高度

无复用方式的单元格高度计算

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UITableViewCell whc_CellHeightForIndexPath:indexPath tableView:tableView];
}

复用方式的单元格高度计算

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UITableViewCell whc_CellHeightForIndexPath:indexPath 
                                             tableView:tableView 
                                            identifier:@"kFirendsCircleCellIdentifier" 
                                           layoutBlock:^(UITableViewCell *cell) {
         /// use model layout cell
         [(FriendsCircleCell *)cell setFriendModel:_friendModelArray[indexPath.row]];
    }];
}

使用 WHC_StackView

创建 WHC_StackView

WHC_StackView * stackView = [WHC_StackView new];
[self.view addSubview: stackView];

添加约束

stackView.whc_LeftSpace(10)
         .whc_TopSpace(10)
         .whc_RightSpace(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 (int i = 0; i < 4; i++) {
    UIView * view = [UIView new];
    [stackView addSubview:view];        
}
[stackView whc_StartLayout];

提示

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

WHC_AutoLayoutKit演示的一部分

image

许可证

所有源代码均采用MIT许可证授权。