masonry 基本用法

时间:2024-11-25 21:34:37

一:masonry 基本用法

    fistView=[[UIView alloc] init];
fistView.backgroundColor=[UIColor redColor];
[self.view addSubview:fistView]; secondView=[[UIView alloc] init];
secondView.backgroundColor=[UIColor blueColor];
[self.view addSubview:secondView]; threeView=[[UIView alloc] init];
threeView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:threeView]; bottomView=[[UIView alloc] init];
bottomView.backgroundColor=[UIColor grayColor];
[self.view addSubview:bottomView];

基本约束布局代码

#pragma mark -第一种布局方法

-(void)left_top_size_marign{

    CGFloat padding =;
CGFloat width=(self.view.bounds.size.width-*padding)/; [fistView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(padding);
make.top.mas_equalTo(padding);
make.size.mas_equalTo(CGSizeMake(width, )); }]; [threeView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(padding*+width);
make.top.mas_equalTo(padding);
make.size.mas_equalTo(CGSizeMake(width, )); }]; [threeView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(padding*+width*);
make.top.mas_equalTo(padding);
make.size.mas_equalTo(CGSizeMake(width, )); }]; }

二:masonry 相对于子View布局

CGFloat padding =;
CGFloat width=(self.view.bounds.size.width-*padding)/; [fistView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(padding);
make.top.mas_equalTo(padding);
make.size.mas_equalTo(CGSizeMake(width, ));
}]; [secondView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(fistView.mas_right).offset(padding);
make.size.top.mas_equalTo(fistView); }]; [threeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(secondView.mas_right).offset(padding);
make.size.top.mas_equalTo(secondView); }];

三:masonry内边距布局

//内边距
[paddingView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(fistView).insets(UIEdgeInsetsMake(, , , )); }];

四:UILable 多行布局

 lb=[[UILabel alloc] init];
lb.text=@"ication:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add remote-notification to the list of your supported";
[self.view addSubview:lb]; --------- //label多行
lb.preferredMaxLayoutWidth=self.view.width-; lb.numberOfLines=;
[lb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(bottomView.mas_bottom).offset();
make.left.mas_equalTo();
make.right.mas_equalTo(-); }];

五:masonry动画更新

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    if(flag){

        CGFloat padding =;
CGFloat width=(self.view.bounds.size.width-*padding)/; [fistView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(padding);
make.top.mas_equalTo(padding);
make.size.mas_equalTo(CGSizeMake(width, ));
}]; }
else{ [fistView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo();
make.top.mas_equalTo();
make.size.mas_equalTo(CGSizeMake(, ));
}];
} flag=!flag; [UIView animateWithDuration:0.25 animations:^{ // [self.view layoutIfNeeded]; } completion:^(BOOL finished) { }]; }