✂ Easy to use and flexible library for manually laying out views and layers for iOS and tvOS. Supports AsyncDisplayKit.

Overview

ManualLayout CocoaPods CocoaPods

Build Status CocoaPods Gitter

Sponsor

Table of Contents

  1. Installation
  2. Usage
  3. API Cheat Sheet

Installation

Carthage

Add the following line to your Cartfile.

github "isair/ManualLayout"

Then do carthage update. After that, add the framework to your project.

CocoaPods

Add the following line in your Podfile.

pod "ManualLayout"

Usage

Just import ManualLayout in your code and use the methods and properties provided by the library to layout your views. You can check out the cheat sheet below for a compact list of everything. There are also example projects to get you started.

API Cheat Sheet

Smart Assign Operator

The smart assign operator =~ has only one job; to make your life easier.

someView.origin =~ (0, 20)
anotherView.size =~ (100, 100)
yetAnotherView.frame =~ (0, 120, view.width, 100)

CGRect/CALayer/UIView Properties

// For fast positioning.
var origin: CGPoint
var x: CGFloat 
var y: CGFloat
var center: CGPoint
var centerX: CGFloat
var centerY: CGFloat
var top: CGFloat
var right: CGFloat
var bottom: CGFloat
var left: CGFloat

// For fast sizing.
var size: CGSize
var width: CGFloat
var height: CGFloat

// Alternate edges. Their names may change in the near future.
var top2: CGFloat
var right2: CGFloat
var bottom2: CGFloat
var left2: CGFloat

The difference between alternate edges and normal edges require a bit of explaining. Imagine we have a view at position (0, 0) of size (100, 100) named myView. If we do myView.right = 200, then its position is now (100, 0) and its size remains unchaged. However, back when our view was located at (0, 0), if we had done myView.right2 = 200, then myView would have still been at (0, 0) but would have had a size of (200, 100).

So basically, setting a normal edge's position drags the whole view along with that edge but setting an alternative edge's position drags just that edge. And don't worry if you, for example, try to drag a left edge past its view's right edge. Edge swapping is done automatically so you don't have to worry about it.

UIView Methods

Just one method with two variants for now, and those are used for easy size calculations.

func sizeToFit(width: CGFloat, height: CGFloat) -> CGSize
func sizeToFit(constrainedSize: CGSize) -> CGSize

So let's say that you have a label inside a view and you want to lay it out with an inset of 4 points on all sides, you could easily do the following:

myLabel.sizeToFit(inset(myView.size, 4))
myLabel.origin =~ (4, 4)

Done!

UIScrollView Properties

var contentWidth: CGFloat
var contentHeight: CGFloat

var contentTop: CGFloat // Always equal to 0. Read-only.
var contentLeft: CGFloat // Always equal to 0. Read-only.
var contentBottom: CGFloat // contentHeight alias.
var contentRight: CGFloat // contentWidth alias.

var viewportTop: CGFloat // contentOffset.y alias.
var viewportLeft: CGFloat // contentOffset.x alias.
var viewportBottom: CGFloat // conentOffset.y + view height
var viewportRight: CGFloat // contentOffset.x + view width

UIViewController Properties

All UIViewController properties are read only. They offer easy read access to a controller's view's properties.

var bounds: CGRect

var center: CGPoint
var centerX: CGFloat
var centerY: CGFloat

var size: CGSize
var width: CGFloat
var height: CGFloat

var top: CGFloat // Top layout guide y coordinate.
var right: CGFloat // Equal to the width of the controller's view.
var bottom: CGFloat // Bottom layout guide y coordinate.
var left: CGFloat // Always equal to 0.

Helper Methods

These functions never modify the view/layer/rectangle/etc they are passed.

func inset(view: UIView, amount: CGFloat) -> CGRect
func inset(layer: CALayer, amount: CGFloat) -> CGRect
func inset(rect: CGRect, amount: CGFloat) -> CGRect

func inset(view: UIView, dx: CGFloat, dy: CGFloat) -> CGRect
func inset(layer: CALayer, dx: CGFloat, dy: CGFloat) -> CGRect
func inset(rect: CGRect, dx: CGFloat, dy: CGFloat) -> CGRect

func inset(view: UIView, top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) -> CGRect
func inset(layer: CALayer, top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) -> CGRect
func inset(rect: CGRect, top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) -> CGRect

func inset(size: CGSize, amount: CGFloat) -> CGSize
func inset(size: CGSize, dx: CGFloat, dy: CGFloat) -> CGSize
func inset(size: CGSize, top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) -> CGSize
func offset(view: UIView, amount: CGFloat) -> CGRect
func offset(layer: CALayer, amount: CGFloat) -> CGRect
func offset(rect: CGRect, amount: CGFloat) -> CGRect

func offset(view: UIView, dx: CGFloat, dy: CGFloat) -> CGRect
func offset(layer: CALayer, dx: CGFloat, dy: CGFloat) -> CGRect
func offset(rect: CGRect, dx: CGFloat, dy: CGFloat) -> CGRect

func offset(point: CGPoint, amount: CGFloat) -> CGPoint
func offset(point: CGPoint, dx: CGFloat, dy: CGFloat) -> CGPoint
You might also like...
FlexiblePickerExample - Example of flexible picker that automatically layouts its own content

Flexible Picker The implementation of custom Flexible Picker. Thanks to that vie

 BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift!
BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift!

BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift! Deprecated BrickKit is being phased out at Wayfair, and the

LayoutKit is a fast view layout library for iOS, macOS, and tvOS.
LayoutKit is a fast view layout library for iOS, macOS, and tvOS.

🚨 UNMAINTAINED 🚨 This project is no longer used by LinkedIn and is currently unmaintained. LayoutKit is a fast view layout library for iOS, macOS, a

LayoutKit is a fast view layout library for iOS, macOS, and tvOS.
LayoutKit is a fast view layout library for iOS, macOS, and tvOS.

🚨 UNMAINTAINED 🚨 This project is no longer used by LinkedIn and is currently unmaintained. LayoutKit is a fast view layout library for iOS, macOS, a

Simple static table views for iOS in Swift.
Simple static table views for iOS in Swift.

Simple static table views for iOS in Swift. Static's goal is to separate model data from presentation. Rows and Sections are your “view models” for yo

FrameLayoutKit is a super fast and easy to use autolayout kit
FrameLayoutKit is a super fast and easy to use autolayout kit

FrameLayoutKit FrameLayout is a super fast and easy to use layout library for iOS and tvOS. For Objective-C version: NKFrameLayoutKit (Deprecated, not

An flexbox layout aimed at easy to use, which depend on Yoga.
An flexbox layout aimed at easy to use, which depend on Yoga.

DDFlexbox A flexbox framework for easy using. Install pod 'DDFlexbox' Template install Recommend using templates to create flexbox views. cd Script/

Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support.
Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support.

CGLayout Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Has cross-hierarchy coordinate space. Implemen

Expose layout margins and readable content width to SwiftUI's Views

SwiftUI Layout Guides This micro-library exposes UIKit's layout margins and readable content guides to SwiftUI. Usage Make a view fit the readable con

Comments
  • Swift 3 branch

    Swift 3 branch

    I think it's time to create a swift-3.0 branch for upcoming Swift 3 release. I can make a PR for adding compatibility for Xcode 8.0 beta 6 :)

    ┆Issue is synchronized with this Asana task

    opened by devxoul 0
  • Correct the spelling of CocoaPods in README

    Correct the spelling of CocoaPods in README

    This pull requests corrects the spelling of CocoaPods ?? https://github.com/CocoaPods/shared_resources/tree/master/media

    Created with cocoapods-readme.

    Review on Reviewable

    ┆Issue is synchronized with this Asana task

    opened by ReadmeCritic 0
  • Add a Gitter chat badge to README.md

    Add a Gitter chat badge to README.md

    isair/ManualLayout now has a Chat Room on Gitter

    @isair has just created a chat room. You can visit it here: https://gitter.im/isair/ManualLayout.

    This pull-request adds this badge to your README.md:

    Gitter

    If my aim is a little off, please let me know.

    Happy chatting.

    PS: Click here if you would prefer not to receive automatic pull-requests from Gitter in future.

    opened by gitter-badger 0
  • Add the ml_ prefix support

    Add the ml_ prefix support

    When use with SwifterSwift, that also has the x/y/width/height property. Can't use them because ambiguous. So add the ml_ prefix support.


    This change is Reviewable

    opened by tospery 0
Releases(1.3.0)
Owner
Baris Sencan
I repeatedly exert minimal force on small pieces of plastic
Baris Sencan
A Swift utility to make updating table views/collection views trivially easy and reliable.

ArrayDiff An efficient Swift utility to compute the difference between two arrays. Get the removedIndexes and insertedIndexes and pass them directly a

Adlai Holler 100 Jun 5, 2022
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

Extremely Fast views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainabl

layoutBox 2.1k Dec 22, 2022
A SwiftUI ScrollView that runs a callback when subviews are scrolled in and out of view.

VisibilityTrackingScrollView This package provides a variant of ScrollView that you can use to track whether views inside it are actually visible. Usa

Elegant Chaos 3 Oct 10, 2022
A custom layout built on top of SwiftUI's Layout API that lays elements out in multiple lines. Similar to flex-wrap in CSS, CollectionViewFlowLayout.

WrapLayout A custom layout built on top of SwiftUI's Layout API that lays elements out in multiple lines. Similar to flex-wrap in CSS, CollectionViewF

Hiroshi Kimura 6 Sep 27, 2022
Apple provides us two ways to use UIKit views in SwiftUI

RepresentableKit Apple provides us two ways to use UIKit views in SwiftUI: UIVie

YUMEMI Inc. 43 Dec 26, 2022
ResponderChainDemo - Learned how to use responder chain for communication between the views

ResponderChainDemo Learned how to use responder chain for communication between

Prashant Gaikwad 1 Sep 30, 2022
Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout

Masonry Masonry is still actively maintained, we are committed to fixing bugs and merging good quality PRs from the wider community. However if you're

null 18k Jan 5, 2023
An iOS app that automatically capitalizes the title of your essay. Supports MLA, APA, and Chicago styles.

Title Capitalizr for iOS An iOS app that automatically capitalizes the title of your essay. Supports MLA, APA, AP, and Chicago styles. "You probably w

John Seong 2 Jun 10, 2022
Compose is a library that helps you compose complex and dynamic views.

Compose is a data driven library that will help compose your complex and dynamic Views. It helps you create complex and dynamic Views using easy and s

OLX Brasil 123 Jun 9, 2021
A flexible collection view with proper horizontal layout flow

FlexCollection A very simple flexible collection view using SwiftUI that automat

null 1 Dec 29, 2021