Git Product home page Git Product logo

bibinjacobpulickal / autolayoutproxy Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 2.0 229 KB

A simple and lightweight Auto-Layout-Kit that makes you feel like playing a fun game laying out views. AutoLayout supports UIKit on iOS & tvOS and AppKit on macOS so you wouldn't have to worry about using different solutions on different platforms. AutoLayoutProxy is compiled on Xcode 14.2 using Swift 5.7 and will be updated for all future releases

License: Other

Ruby 0.93% Swift 98.49% C 0.58%
xcode cocoapods swift constraints subview uikit autolayout autolayout-constraints swift-package-manager snapkit

autolayoutproxy's Introduction

AutoLayoutProxy

A simple and lightweight Auto-Layout Kit that makes you feels laying out views as a fun game. AutoLayoutProxy supports UIKit on iOS & tvOS and AppKit on MacOS so you wouldn't have to worry about using different solutions on different platforms. AutoLayoutProxy is compiled on Xcode 12.0 using Swift 5.7 and will be updated for all future releases.

Platform Platform Language Swift version Swift License

Why use AutoLayoutProxy?

Have you ever had to write lines and lines of code to add subviews and anchor them like the following:

view.addSubview(subview)
subview.translatesAutoresizingMaskIntoConstraints = false

subview.topAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
...
subview.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

AutoLayoutProxy v4 introduces declarative UI Layout syntax

view.addSubview(subview) {
    subview.top.equalTo(view.top).constant(16)
    $0.leading.lessThanOrEqualTo($1.leading).constant(16)
    $0.height.equalTo(100)
    $0.width.greaterThanOrEqualTo($1.width).multiplier(0.8).constant(-12)
}

Worry no more AutoLayoutProxy helps you with this situation as follows:

view.addSubview(subview) {
    subview.top      == view.bottom
    ...
    subview.trailing == view.trailing
}
// Or in short ๐Ÿ™‚
view.addSubview(subview) { $0.sides == $1.sides }

// Or even shorter ๐Ÿ˜Ž
view.addSubview(subview) { $0 === $1 } 

You see thats all thats required. Now, you might be wondering what would you do if you had to make a constraint with a relation that's not equal but, less than or greater than, like the following:

subview.leadingAnchor.constraint(lessThanOrEqualTo: view.leadingAnchor, constant: -8).isActive = true
subview.trailingAnchor.constraint(greaterThanOrEqualTo: view.trailingAnchor: constant: 16).isActive = true

AutoLayoutProxy let's you do this:

subview.leading  <= view.leading
subview.trailing >= view.trailing

In the case of constants you can add or substract them in the same expresion, so instead of this:

subview.topAnchor.constraint(equalTo: view.bottomAnchor, constant: 16).isActive = true
subview.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: -32).isActive = true

You only need this: subview.top == view.bottom + 16 subview.leading == view.trailing - 32 If you would like to get the constraint for animations or reference in future, just do:

let subviewTopConstraint      = subview.top == view.bottom + 16
subviewTopConstraint.constant = 32

If you don't want the constraints to not be active on the first go just do, '!=' or even '!<=' and '!>=', like the following:

subview.top      != view.bottom
subview.leading  !<= view.leading
subview.trailing !>= view.trailing

In case of dimensions like height or width:

subview.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 2, constant: 8).isActive = true
subview.heightAnchor.constraint(equalToConstant: 64).isActive = true

You only need this:

subview.width  == view.width * 2 + 8
subview.height == 64

For addSubview and translatesAutoresizingMaskIntoConstraints = false you could just write a closure as follows:

view.addSubview(subview) {
  subview.sides <= view.sides
}

You also have other helpers like sides that include all sides, and size which takes a CGSize and also both horizontal and vertical constraints and centers.

// Constraint all sides with a padding of 2
subview.sides == view.sides + 2

// Constraint all sides with different padding on each side.
subview.sides == view.sides + UIEdgeInsets(t: 1, l: 2, b: 4, r: 8)

// Align both centers of view and subview
subview.centers == view.centers

// Constraint both centers with offsets 4 and 8.
subview.centers == view.centers + UIOffset(x: 4, y: 8)

// Constraint vertical(top and bottom) or horizontal(leading and trailing)
subview.vertical   == view.vertical
subview.horizontal == view.horizontal

// Constraint size either with CGSize or both sides 64
subview.size == CGSize(w: 8, h: 16)
subview.size == 64

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but BBLoader does support its use on supported platforms.

Once you have your Swift package set up, adding BBLoader as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/bibinjacobpulickal/AutoLayoutProxy.git", .upToNextMajor(from: "4.0.0"))
]

From Xcode 11.0+ you only need to provide the above mentioned URL.

Note: No longer need to provide the above URL. You can search for AutoLayoutProxy package within Xcode.

Features

  • Remove repetitive code while creating new view.
  • Fill super view or any other view with/without padding.
  • Align center with super view or any other view.

Support

  • iOS 9.0+
  • Xcode 8.3+
  • Swift 3.0+

License

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

autolayoutproxy's People

Contributors

bibin-fingent avatar bibinjacobpulickal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

batman-jacob

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.