iOS masonry设置六个等高等宽的view

该博客介绍了如何在iOS中利用Masonry框架为父视图添加六个颜色各异且等宽等高的子视图。通过示例代码展示了如何设置约束,使得这些子视图在垂直方向上均匀分布,并在水平方向上两两对齐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#import "ThreeViewController.h"
#import "View+MASAdditions.h"
#import "UIView+Masonry_LR.h"
#import "FourViewController.h"
static NSString const*kStoryboardName=@"ThreeViewController";
static NSString const*kIdentifier=@"ThreeViewController";
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

@interface ThreeViewController ()

@end

@implementation ThreeViewController

+ (instancetype)createViewControllerWithStoryboard:(id)createArgs{
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:(NSString *)kStoryboardName bundle:nil];
    ThreeViewController *vc=[storyboard instantiateViewControllerWithIdentifier:(NSString *)kIdentifier];
    return vc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"第三个约束"];
    [self initView];
    [self initRight];
}
- (void)initRight{
    UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithTitle:@"第四个约束" style:UIBarButtonItemStylePlain target:self action:@selector(right)];
    self.navigationItem.rightBarButtonItem=right;
}
- (void)right{
    FourViewController *vc=[FourViewController createViewControllerWithStoryboard:nil];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)initView{
    WS(ws);
    UIView *view=[UIView new];
    view.backgroundColor=[UIColor brownColor];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(ws.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
    }];
    
    UIView *view1 = [UIView new];
    UIView *view2 = [UIView new];
    UIView *view3 = [UIView new];
    UIView *view4 = [UIView new];
    UIView *view5 = [UIView new];
    UIView *view6 = [UIView new];
    
    view1.backgroundColor = [UIColor redColor];
    view2.backgroundColor = [UIColor blueColor];
    view3.backgroundColor = [UIColor cyanColor];
    view4.backgroundColor = [UIColor yellowColor];
    view5.backgroundColor = [UIColor blackColor];
    view6.backgroundColor = [UIColor lightGrayColor];
    
    [view addSubview:view1];
    [view addSubview:view2];
    [view addSubview:view3];
    [view addSubview:view4];
    [view addSubview:view5];
    [view addSubview:view6];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@[view2]);
        make.centerX.equalTo(@[view3,view5]);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@[view1]);
        make.centerX.equalTo(@[view4,view6]);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@[view4]);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    
    [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@[view3]);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    
    [view5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@[view6]);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    [view6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    
    [view distributeSpacingHorizontallyWith:@[view1,view2]];
    [view distributeSpacingVerticallyWith:@[view1,view3,view5]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值