在代码中布局您的 UI。没有技巧,没有荒唐事,只有简单的布局。
#import <PTRManualLayout/PTRManualLayout.h>
PTRMLRects
(请参阅下面的 示例) 在 layoutSubviews
中构建您的布局,通常[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 文件以获取更多信息。