PTRManualLayout 0.8.0

PTRManualLayout 0.8.0

测试已测试
语言语言 Obj-CObjective C
许可协议 Apache 2
发布日期最后发布2016年5月

David Kettler 维护。



  • Sam Morrison 和 David Kettler

CI Status

在代码中布局您的 UI。没有技巧,没有荒唐事,只有简单的布局。

目录

  1. 示例
  2. 安装
  3. 用法

用法

  1. #import <PTRManualLayout/PTRManualLayout.h>
  2. 通过将视图映射到 PTRMLRects (请参阅下面的 示例) 在 layoutSubviews 中构建您的布局,通常
  3. [layout apply] 使所有您的愿望成为现实

示例

要运行示例项目,请克隆仓库,然后首先从 Example 目录中运行 pod install

#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 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile

pod "PTRManualLayout"

作者

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

许可协议

PTRManualLayout 适用于 Apache 2.0 许可协议。请参阅 LICENSE 文件以获取更多信息。