Swiftpack.co -  foxsin10/UStack as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
foxsin10/UStack
Use less code to layout views in UIKit or Cocoa
.package(url: "https://github.com/foxsin10/UStack.git", from: "1.0.4")

UStack

A tool for StackView in UIKit.

Requirements

  • iOS 11.0+
  • Xcode 12.5+

Use cases

HStack use-case:

let label = UILabel()
let button = UIButton()
let simpleView = UIView()

let hstack = HStack {
    label
    button
    simpleView
}

VStack use-case:

let label = UILabel()
let button = UIButton()
let simpleView = UIView()

let vstack = VStack {
    label
    button
    simpleView
}

Nested use-case:

let refresh = Bool.random()
let refreshAnother = Bool.random()
let idx = Int.random(in: 1 ... 12)
HStack {
  VStack {
    UILabel()
    UIButton()
  }
  if refresh {
    VStack {
      UIImageView()
      UIView()
    }
  }
  if refreshAnother {
    VStack {
      UIImageView()
      UIView()
    }
  } else {
      UIView()
  }
  UIView().withSubViews {
    HStack {
      UILabel()
      UIButton()
    }
  }
}

ps: every control flow will be treated as a block, give the simplest VStack for example:

  • buildExpression(_:) -> UILabel
  • buildExpression(_:) -> UIButton
  • buildBlock(_:) -> VStack
  • buildExpression(_:) -> VStack

StackBuilder use-case:

@HStackBuilder
func hstack() -> UIStackView {
  UILabel()
  UIButton()
}

@VStackBuilder
func vstack() -> UIStackView {
  UILabel()
  UIButton()
}

Sample

UStack

As the example image above, the implement code:

private lazy var registerActionStack = VStack(spacing: minorSpacing, alignment: .fill) {
  registerButton
  UIView.spacer()
  loginButton
}

private lazy var registerPolicyStack = HStack(spacing: mediumSpacing) {
  UIView.spacer()
  policyCircleButton
  tipLabel
  policyInfoButton
  UIView.spacer()
}

private lazy var contentStack = VStack(spacing: mainMargin, alignment: .center) {
  sloganImageView
  loginLogoImageView
  registerActionStack
  UIView.spacer()
  registerPolicyStack
}

and the layout code:

view.withSubViews {
  contentStack
}

contentStack.snp.makeConstraints {
  $0.top.equalTo(view.safeAreaLayoutGuide.snp.top)
        .offset(topPadding)
  $0.leading.trailing.equalToSuperview()
        .inset(horizontalPadding)
  $0.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
}

registerButton.snp.makeConstraints {
  $0.width.equalTo(view)
          .inset(horizontalPadding)
}

Installation

Swift Package Manager

  • File > Swift Packages > Add Package Dependency
  • Add https://github.com/foxsin10/UStack.git
  • Select "Up to Next Major" with "2.0.0"

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!

target 'MyApp' do
  pod 'UStack', '~> 1.0.4'
end

License

UStack is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 1
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API