CALayer-AutoresizingMask

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


License
MIT
Install
pod try CALayer-AutoresizingMask

Documentation

CALayer-AutoresizingMask

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

Installation

pod 'CALayer-AutoresizingMask'

Usage

  1. Do not use Autolayout in this storyboard or XIB and use autoresizing mask you need.

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

  1. 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 axis. 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 ...