CALayer-AutoresizingMask 0.0.7

CALayer-AutoresizingMask 0.0.7

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Jan 2015

Maintained by Anton Bukov.



 
Depends on:
JRSwizzle>= 0
ObjcAssociatedObjectHelpers>= 0
 

Add UIViewAutoresize support to iOS CALayers and fast UIView to CALayer conversion method

Installation

pod 'CALayer-AutoresizingMask'

Usage

  1. Not use Autolayout in this storyboard or xib and use autoresizing mask you need.

  2. Hot swap non touchable UIViews with CALayers in IB. Remember it applies recursively to all subviews.

  3. Use your old IBOutlets to views to access visible layers :)

    Now all CALayers have property autoresizingMask with type UIVIewAutoresizing. You can use it too!

How it works?

It just implements simple algorithm:

Increase origin and size of self.frame proportionaly to superviews frame increment divided by number of flexible elements on each axe. That is all!

CGFloat dx = self.superlayer.bounds.size.width - self.superlayerSize.width;
CGFloat dy = self.superlayer.bounds.size.height - self.superlayerSize.height;

dx /= ((mask & UIViewAutoresizingFlexibleLeftMargin)?1:0)
    + ((mask & UIViewAutoresizingFlexibleWidth)?1:0)
    + ((mask & UIViewAutoresizingFlexibleRightMargin)?1:0);
dy /= ((mask & UIViewAutoresizingFlexibleTopMargin)?1:0)
    + ((mask & UIViewAutoresizingFlexibleHeight)?1:0)
    + ((mask & UIViewAutoresizingFlexibleBottomMargin)?1:0);

CGRect frame = self.frame;
frame.origin.x += (mask & UIViewAutoresizingFlexibleLeftMargin)?dx:0;
frame.origin.y += (mask & UIViewAutoresizingFlexibleTopMargin)?dy:0;
frame.size.width += (mask & UIViewAutoresizingFlexibleWidth)?dx:0;
frame.size.height += (mask & UIViewAutoresizingFlexibleHeight)?dy:0;
self.frame = frame;

Contribute

You are welcome to fork, PR, create issues ...