Swift 轻量级 AutoLayout 开源库

978 阅读2分钟
原文链接: github.com


Build Status Pod Version Platform Pod License

  • iOS and Mac OS X platforms currently in use the fastest the simplest development to build the UI layout automatically open source library, strong dynamic layout constraint handling capacity
  • Service to update constraints, convenient and quick dynamic UI layout.

Objective-c version 👉 WHC_AutoLayout

Introduce

  • Adopt chain layout Api calls convenient
  • Include one line of code to calculate UITableViewCell highly module
  • Contains WHC_StackView module (UIStackView purpose alternative system)
  • Automatic identification of the same type conflict and update the new constraints
  • Support change constraints priority
  • Support delete constraints
  • Support iOS and Mac OS X
  • Automatic covering and modify the conflict with type constraints

Require

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+
  • Xcode 8.0 or later
  • Swift 3.0+

Install

  • CocoaPods: pod 'WHC_Layout'

Usage

Automatic height view

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

SnapKit/Masonry update the constraint way unfriendly

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)
}

Update the view constraints

Modify the view to the left from 20 other views

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

Can be directly modified constraints on the Xib and Storyboard

If the view of xib leading now amended as left constraints

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

Remove the constraint

Remove all constraints associated with view left

view.whc_RemoveAttrs(.left)

To remove multiple constraints associated with view

view.whc_RemoveAttrs(.left, .leading, .top)

Modify the view constraint priority

Modify the view constraint for low priority right

view.whc_Right(10)
    .whc_PriorityLow()

One line of code calculation cell height

No reuse way calculated cell height

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

Reuse way calculated cell height

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)
    })
}

Use WHC_StackView

Create WHC_StackView

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

Add constraint

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

Configuration stackView

1. Set the padding

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

2. Set the layout direction

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

3. Set the child views lateral clearance

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

4. Set the child views vertical clearance

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

5. Add subview and start the layout

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

Prompt

For more UI layout automatically, WHC_StackView components, one line of code to calculate the cell height module, please download this demo to check the specific usage

Licenses

All source code is licensed under the MIT License.