Masony子视图撑起父视图高度

2021-05-31  本文已影响0人  OrrHsiao

父视图不约束宽高,子视图设置好距离父视图的各边边距,并指定子视图的宽高(两者缺一不可)。
父视图

    UIView *superView = [[UIView alloc] init];
    [superView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:superView];
    [superView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
    }];

子视图

    UIView *childView = [[UIView alloc] init];
    [childView setBackgroundColor:[UIColor grayColor]];
    [superView addSubview:childView];
    [childView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(10);
        make.right.bottom.offset(-10);
        make.width.height.offset(100);
    }];
上一篇 下一篇

猜你喜欢

热点阅读