A simpler way to work with NSLayoutAnchor

Overview

Banner

CI Status Version License Platform

SugarAnchor is syntactic sugar on NSLayoutAnchor to help us write more compact, readable and easy layout code. It wraps up all of NSLayoutXAxisAnchor, NSLayoutYAxisAnchor and NSLayoutDimension functionalities under some easy to use operators to reduce verbosity.

Features

  • Simple, concise, native[1]. Almost zero learning curve
  • Typesafe, similar to NSLayoutAnchor
  • Unit tested
  • Small codebase (less than 300 LOC)

[1] Same NSLayoutAnchor/NSLayoutConstraints, just syntactic sugar on it

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • Xcode 8.3 or above
  • Swift 3.1
  • iOS 9.0 or above

Installation

SugarAnchor is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SugarAnchor"

Operator Summary

Let's looks at a simple NSLayoutAnchor code:

(redView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20)).isActive = true

With SugarAnchor, it become:

redView.leadingAnchor =*= view.leadingAnchor + 20

Think * as Active constraint and ~ as Inactive constraint. Then with =*=, you'll create an active constraint directly or with =~= you may create an inactive constraint which you can activate later.

view1.leftAnchor =*= view2.leftAnchor + 10

// Or

let leftConstraint = (view1.leftAnchor =~= view2.leftAnchor + 10)
leftConstraint.isActive = true

In each case, you'll get the constraint to keep or just ignore. For example, for an active one:

self.heightConstraint = (v1.heightAnchor =*= 200)
// Later somewhere
self.heightConstraint.constant = 100

Operator list

Operator Description Example
=*= Equal
(Active)
v1.leadingAnchor =*= v2.leadingAnchor
v1.leftAnchor =*= v2.leftAnchor + 20
v1.widthAnchor =*= v2.widthAnchor / 2 + 10
v1.heightAnchor =*= 200
<*= LessThanOrEqual
(Active)
v1.bottomAnchor <*= container.bottomAnchor - 8
>*= GreaterThanOrEqual
(Active)
v2.leadingAnchor >*= v1.trailingAnchor + 5
=~= Equal
(Inactive)
(v1.widthAnchor =~= 200).isActive = true
<~= LessThanOrEqual
(Inactive)
(v1.bottomAnchor <~= container.bottomAnchor - 8).isActive = true
>~= GreaterThanOrEqual
(Inactive)
(v2.leadingAnchor >~= v1.trailingAnchor + 5).isActive = true

Author

ashikahmad, [email protected]

License

SugarAnchor is available under the MIT license. See the LICENSE file for more info.

You might also like...
A script for focusing on work, blocking non-work stuff.

A script for focusing on work, blocking non-work stuff. The idea is to forbid mindless app/website context-switching while you're focused. Once you're

Work-hours-mac - Simple app that tracks your work hours from the status bar
Work-hours-mac - Simple app that tracks your work hours from the status bar

Track Your Work Hours Simple app that tracks your work hours from status bar. Fe

Tools for making SwiftUI navigation simpler, more ergonomic and more precise.
Tools for making SwiftUI navigation simpler, more ergonomic and more precise.

SwiftUI Navigation Tools for making SwiftUI navigation simpler, more ergonomic and more precise. Motivation Tools Navigation overloads Navigation view

PdfBuilder: a swift library made to make creation of the Pdf file from code simpler
PdfBuilder: a swift library made to make creation of the Pdf file from code simpler

PdfBuilder PdfBuilder is a swift library made to make creation of the Pdf file f

Date Formatter Pool - is a small utility that creates and stores your Date Formatter for simpler reuse
Date Formatter Pool - is a small utility that creates and stores your Date Formatter for simpler reuse

Date Formatter Pool Date Formatter Pool - is a small utility that creates and stores your Date Formatter for simpler reuse Installation is available i

Special way to work with gestures in iOS
Special way to work with gestures in iOS

At a Glance Sensitive is a library that simplifies work with gestures in iOS. Forget about target/action pattern of primitive UIGestureRecognizer. Wit

Easy way to detect iOS device properties, OS versions and work with screen sizes. Powered by Swift.
Easy way to detect iOS device properties, OS versions and work with screen sizes. Powered by Swift.

Easy way to detect device environment: Device model and version Screen resolution Interface orientation iOS version Battery state Environment Helps to

Special way to work with gestures in iOS
Special way to work with gestures in iOS

At a Glance Sensitive is a library that simplifies work with gestures in iOS. Forget about target/action pattern of primitive UIGestureRecognizer. Wit

RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

A Github's like work contribution timeline. 🤸🏻
A Github's like work contribution timeline. 🤸🏻

Workaholic 🤸🏻 A Github's like work contribution timeline. ToDo[s] Remove Dependancies of 3rd Party classes. You can watch to Workaholic to see conti

How Swift standard types and classes were supposed to work.
How Swift standard types and classes were supposed to work.

How Swift standard types and classes were supposed to work. A collection of useful extensions for the Swift Standard Library, Foundation, and UIKit.

Fully-wrapped UITextField made to work entirely in SwiftUI
Fully-wrapped UITextField made to work entirely in SwiftUI

iTextField ⌨️ A fully-wrapped `UITextField` that works entirely in SwiftUI. 🦅 Get Started | Examples | Customize | Install | Get Started Install iTex

SimpleAuth is designed to do the hard work of social account login on iOS

SimpleAuth is designed to do the hard work of social account login on iOS. It has a small set of public APIs backed by a set of "providers"

RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

LogDog is designed to work out of the box, you can use the pre-configured logger anytime, anywhere
LogDog is designed to work out of the box, you can use the pre-configured logger anytime, anywhere

LogDog user-friendly logging apple/swift-log api compatible Usage LogDog is designed to work out of the box, you can use the pre-configured logger any

Extensions that allow you to work with optionals

RxOptionals Sometimes it happens that you need to bind to several optional binders or to an optional method (for example, when using weak). To do this

Work in progress gallery of controls available to Catalyst apps using Optimized for Mac
Work in progress gallery of controls available to Catalyst apps using Optimized for Mac

Catalyst Controls Gallery Very simple work-in-progress demonstration of many common controls available to Mac Catalyst as of macOS 11. Provided moreso

GraphQLite is a toolkit to work with GraphQL servers easily. It also provides several other features to make life easier during iOS application development.
GraphQLite is a toolkit to work with GraphQL servers easily. It also provides several other features to make life easier during iOS application development.

What is this? GraphQLite is a toolkit to work with GraphQL servers easily. It also provides several other features to make life easier during iOS appl

Pre-work: A tip calculator application for iOS
Pre-work: A tip calculator application for iOS

Pre-work - Tip Calculator Tip Calculator is a tip calculator application for iOS. Submitted by: Jonathan Ballona Sanchez Time spent: 7 hours spent in

Releases(0.1.0)
Owner
Ashik Uddin Ahmad
Ashik Uddin Ahmad
An extension that simplifies the work with Swift AutoLayout by writing cleaner, more natural and easy-reading code.

Installation For now you're able to clone repo in your project or download ZIP folder manually. git clone https://github.com/votehserxam/AutoLayout.gi

Max 3 Nov 8, 2022
An easy way to create and layout UI components for iOS (Swift version).

Introduction Cupcake is a framework that allow you to easily create and layout UI components for iOS 8.0+. It use chaining syntax and provides some fr

nerdycat 288 Oct 9, 2022
An easier and faster way to code Autolayout

EZAnchor 中文介绍 An easier way to code Autolayout Are you annoyed of coding .active = true while using Autolayout Anchors over and over again? Are you an

Alex.Liu 25 Feb 20, 2022
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.

QuickLayout QuickLayout offers an additional way, to easily manage the Auto Layout using only code. You can harness the power of QuickLayout to align

Daniel Huri 243 Oct 28, 2022
A simple integrated version of iOS 13 Compositional Layout, modified into a way similar to Functional Programming to generate UICollectionViewCompositionalLayout.

WWCompositionalLayout A simple integrated version of iOS 13 Compositional Layout, modified into a way similar to Functional Programming to generate UI

William-Weng 1 Jul 4, 2022
Easiest way to load view classes into another XIB or storyboard.

LoadableViews Easiest way to load view classes into another XIB or storyboard. Basic setup Subclass your view from LoadableView Create a xib file, set

MLSDev 43 Nov 4, 2022
Async State Machine aims to provide a way to structure an application thanks to state machines

Async State Machine Async State Machine aims to provide a way to structure an application thanks to state machines. The goal is to identify the states

Thibault Wittemberg 27 Nov 17, 2022
Creating an iOS Library the Right Way

AppCircleSampleProject Installation Cocoapods AppCircleSampleProject is available through CocoaPods. To install it, simply add the following line to y

Ferhan Akkan 3 Aug 1, 2022
Launches unc0ver in a way that works when it's installed through TrollStore

u0Launcher Normally when you install unc0ver through TrollStore and try to jailbreak with it, it fails with a "Failed to disable codesigning" error be

Lars Fröder 55 Dec 21, 2022
A simpler way to do cool UITableView animations! (╯°□°)╯︵ ┻━┻

TableFlip (╯°□°)╯︵ ┻━┻ ┬──┬ ノ( ゜-゜ノ) Animations are cool. UITableView isn't. So why not make animating UITableView cool? The entire API for TableFlip

Joe Fabisevich 555 Dec 9, 2022