PTRManualLayout 0.8.0

PTRManualLayout 0.8.0

TestsTested
LangLanguage Obj-CObjective C
License Apache 2
ReleasedLast Release May 2016

Maintained by David Kettler.



  • By
  • Sam Morrison and David Kettler

CI Status

Layout your UIs in code. No tricks, no funny business, just dead simple layout.

Table of Contents

  1. Example
  2. Installation
  3. Usage

Usage

  1. #import <PTRManualLayout/PTRManualLayout.h>
  2. Build your layout, typically in layoutSubviews, by mapping your views to PTRMLRects (see Example below)
  3. [layout apply] to make all your dreams come true

Example

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

#import "ExampleView.h"
#import <PTRManualLayout/PTRManualLayout.h>

@interface ExampleView ()
@property (nonatomic, strong) UILabel *prompt;
@property (nonatomic, strong) UIButton *button;
@end

@implementation ExampleView

- (instancetype)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    self.prompt = [[UILabel alloc] initWithFrame:CGRectZero];
    self.prompt.text = @"PTRManualLayout lets you lay out your views with ease";
    [self addSubview:self.prompt];

    self.button = [[UIButton alloc] initWithFrame:CGRectZero];
    [self.button setTitle:@"Learn More"
                 forState:UIControlStateNormal];
    [self addSubview:self.button];
  }
  return self;
}

- (void)layoutSubviews {
  [super layoutSubviews];
  [[self buildLayout:self.bounds.size] apply];
}

- (CGSize)sizeThatFits:(CGSize)size {
  return [[self buildLayout:size] containingRect].size;
}

- (PTRMLLayout *)buildLayout:(CGSize)size {
  PTRMLLayout *layout = [MLLayout layoutWithBounds:(CGRect){CGPointZero, size}];

  CGFloat padding = 10;
  PTRMLLayout *insetRect = [PTRMLLayout rectWithCGRect:CGRectInset(layout.bounds.frame, padding, padding)];

  layout[self.prompt].size = [self.prompt sizeThatFits:insetRect.size];
  layout[self.prompt].centerX = insetRect.centerX;

  layout[self.button].size = [self.button sizeThatFits:insetRect.size];
  layout[self.button].centerX = insetRect.centerX;

  CGFloat totalHeightWithPadding = layout[self.prompt].height + padding + layout[self.button].height;
  layout[self.prompt].top = insetRect.centerY - (totalHeightWithPadding / 2);
  layout[self.button].bottom = insetRect.centerY + (totalHeightWithPadding / 2);

  return layout;
}

@end

Installation

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

pod "PTRManualLayout"

Author

Sam Morrison, [email protected] David Kettler, [email protected]

License

PTRManualLayout is available under the Apache 2.0 license. See the LICENSE file for more info.