Swiftpack.co - carloshmartins/CHViewCodable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by carloshmartins.
carloshmartins/CHViewCodable 1.0.0
CHViewCodable is designed to be used with any iOS Projects based on View-Code.
⭐️ 0
🕓 3 days ago
iOS
.package(url: "https://github.com/carloshmartins/CHViewCodable.git", from: "1.0.0")

CHViewCodable

SPM Platforms

CHViewCodable is designed to be used with any iOS Projects based on View-Code

  • ✅ LayoutDimension

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

  • This code is heavily inspired by https://github.com/SnapKit/SnapKit and some internal tools used in my past jobs. This is another Autolayout proxy of thousands out there, but I used this as my own for my own UIKit-based projects.

Installing

Make sure Xcode is installed first and then add this SPM via git url inside Xcode, just like any other Package.

Use as dependency

Add the following to your Package.swift file's dependencies:

.package(url: "https://github.com/carloshmartins/CHViewCodable", from: "1.0.0"),

And then import wherever needed: import CHViewCodable

Feel free to add your own!

How-to

Import the Package into your desired View

import CHViewCodable

Call this function

yourView.layout(using: LayoutProxy)

Two ways of using the function

  view.layout { proxy in
      proxy.top == topAnchor
      proxy.leading == leadingAnchor
      proxy.trailing == trailingAnchor
      proxy.bottom == bottomAnchor
  }

or

  view.layout {
      $0.top == topAnchor
      $0.leading == leadingAnchor
      $0.trailing == trailingAnchor
      $0.bottom == bottomAnchor
  }

Example

import UIKit
import CHViewCodable

class TestingView: UIView {
    
    private lazy var view: UIView = {
       let view = UIView()
        
        return view
    }()
    
    private lazy var topConstraint = NSLayoutConstraint()
    private lazy var leadingConstraint = NSLayoutConstraint()
    private lazy var trailingConstraint = NSLayoutConstraint()
    private lazy var bottomConstraint = NSLayoutConstraint()
    
    func build() {
        // Without offset
        view.layout {
            $0.top == topAnchor
            $0.leading == leadingAnchor
            $0.trailing == trailingAnchor
            $0.bottom == bottomAnchor
        }
        
        // With offset
        view.layout {
            $0.top == topAnchor + 50
            $0.leading == leadingAnchor + 50
            $0.trailing == trailingAnchor - 50
            $0.bottom == bottomAnchor - 50
        }
        
        // With Constraints, if you want to customize different priority or so
        view.layout {
            topConstraint = $0.top.equal(to: topAnchor, priority: .defaultHigh)
            leadingConstraint = $0.leading.equal(to: leadingAnchor)
            trailingConstraint = $0.leading.equal(to: leadingAnchor)
            bottomConstraint = $0.bottom.equal(to: bottomAnchor)
        }
    }
}

GitHub

link
Stars: 0
Last commit: 12 hours ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

3 days ago

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