父视图不约束宽高,子视图设置好距离父视图的各边边距,并指定子视图的宽高(两者缺一不可)。
父视图
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);
}];
网友评论