Giter VIP home page Giter VIP logo

manuallayout's Introduction

ManualLayout CocoaPods CocoaPods

Build Status CocoaPods Gitter

Sponsor

Table of Contents

  1. Installation
  2. Usage
  3. API Cheat Sheet

Installation

Add the following line to your Cartfile.

github "isair/ManualLayout"

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

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

manuallayout's People

Contributors

apouche avatar devxoul avatar isair avatar readmecritic avatar sebastiencouture avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

manuallayout's Issues

Add convenience scripts

install.sh
install-fast.sh
install-travis.sh
test.sh
pod-lint.sh
pod-push.sh

Modify .travis.yml afterwards.

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

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.