PureLayout Learn

161 阅读1分钟

设置大小

方式1
[self.redView autoSetDimensionsToSize:CGSizeMake(100, 100)];

方式2
[redView autoSetDimension:ALDimensionWidth toSize:100];
[redView autoSetDimension:ALDimensionHeight toSize:100];

控件之间间距设置

//蓝色view的左边,距离红色view的右边为30
[self.blueView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.redView withOffset:30];

控件之间等高 或者 等宽

 [@[self.blueView,self.redView] autoMatchViewsDimension:ALDimensionWidth];
 [@[self.blueView,self.redView] autoMatchViewsDimension:ALDimensionHeight];

控件之间水平对齐

[self.blueView autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.redView];

//红色view在父view的中心点
[self.redView autoCenterInSuperview];

父子控件间距

[self.greenView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(15, 15, 15, 15)];

[self.greenView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];

单个间距设置  如left
[self.purpleView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20.f];

父子控件一样大小

[self.greenView autoPinEdgesToSuperviewEdges];

控件之间高度一半

//蓝色View的高度是红色View高度的一半
[self.blueView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.redView withMultiplier:0.5];

控件如何创建

UIImageView *contentView = [UIImageView newAutoLayoutView];

多个控件之间水平布局

@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UIView *blueView;
@property (nonatomic, strong) UIView *purpleView;
@property (nonatomic, strong) UIView *greenView;

@end

@implementation ThirdDemoController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"ThirdDemo";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.redView];
    [self.view addSubview:self.blueView];
    [self.view addSubview:self.purpleView];
    [self.view addSubview:self.greenView];
    
    [self initThirdPureLayout];
}
#pragma mark - 设置约束
- (void)initThirdPureLayout{
    NSArray *colorViews = @[self.redView, self.blueView, self.purpleView, self.greenView,];
    [colorViews autoSetViewsDimension:ALDimensionHeight toSize:40.f];
    //间距为10,水平对齐,依次排列
    [colorViews autoDistributeViewsAlongAxis:ALAxisHorizontal alignedTo:ALAttributeHorizontal withFixedSpacing:10.0 insetSpacing:YES matchedSizes:YES];
    //红色view相对于其父view水平对齐
    [self.redView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
}

需要后面搞清楚

 [_purpleView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.contentView withOffset:0 relation:NSLayoutRelationLessThanOrEqual];