【iOS知识汇】masonry总结 父控件如何自适应子控件。
父控件如何自适应子控件。
比如 父控件加入了几个不知宽度在控件。赋值后,我们需要父控件宽度值。那怎么办?
开发中,对子控件都加入了Mas约束。显示正常。
UIView* jdAdview =[UIView new]; [bottomBarBg addSubview:jdAdview]; [jdAdview mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(bottomBarBg); make.left.offset(15); }];
父控件没有指定宽度。结果。父控件宽度是0.显然不是我要的。
经思考修改:
在子控件最右的约束中加了下面的代码。结果父控年就有宽度了。
make.right.equalTo(jdAdview.mas_right);
控件想要有frame值。依赖别人。或者被依赖。
//京东报价 UIView* jdAdview =[UIView new]; [bottomBarBg addSubview:jdAdview]; [jdAdview mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(bottomBarBg); make.left.offset(15); }]; UIImageView*ivIcJd = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"jd_logo"]]; [jdAdview addSubview:ivIcJd]; [ivIcJd mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(jdAdview); make.left.equalTo(bottomBarBg).offset(15); }]; UILabel *lbJd = [[UILabel alloc] init]; lbJd.text = @"京东"; lbJd.font = [UIFont systemFontOfSize :14]; lbJd.textColor = [UIColor colorWithRed:71/255.0 green:71/255.0 blue:71/255.0 alpha:1/1.0]; [jdAdview addSubview:lbJd]; UILabel *lbMoeny = [[UILabel alloc] init]; lbMoeny.text = @"¥"; lbMoeny.font = [UIFont systemFontOfSize:11]; lbMoeny.textColor = [UIColor colorWithRed:236/255.0 green:47/255.0 blue:47/255.0 alpha:1/1.0]; [jdAdview addSubview:lbMoeny]; UILabel *lbPrice = [[UILabel alloc] init]; lbPrice.frame = CGRectMake(90, 1026, 39, 22); lbPrice.text = @"3168"; lbPrice.font = [UIFont systemFontOfSize:16]; lbPrice.textColor = [UIColor colorWithRed:236/255.0 green:47/255.0 blue:47/255.0 alpha:1/1.0]; [jdAdview addSubview:lbPrice]; [lbJd mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(jdAdview); make.left.equalTo(ivIcJd.mas_right).offset(10); }]; [lbMoeny mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(jdAdview).offset(2); make.left.equalTo(lbJd.mas_right).offset(6); }]; [lbPrice mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(jdAdview); make.left.equalTo(lbMoeny.mas_right); make.right.equalTo(jdAdview.mas_right); }];