TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | Apache 2 |
ReleasedLast Release | May 2016 |
Maintained by David Kettler.
Layout your UIs in code. No tricks, no funny business, just dead simple layout.
#import <PTRManualLayout/PTRManualLayout.h>
layoutSubviews
, by mapping your views to PTRMLRects
(see Example below)[layout apply]
to make all your dreams come trueTo 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
PTRManualLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "PTRManualLayout"
Sam Morrison, [email protected] David Kettler, [email protected]
PTRManualLayout is available under the Apache 2.0 license. See the LICENSE file for more info.