backgroundView 为scrollview 全局定义了
代码直接复制到项目里就可以看滚动效果 注释是特别需要注意的
代码有的空格需要自己加 这边格式会自动对齐 我稍微改了改
中间控件随便加 上下控件要加好 左右一定不要设置为scrollview的左右
- (void)setUI{
backgroundView= [[UIScrollView alloc]init];
[self.view addSubview:backgroundView];
UIView*bottomView = [[UIViewalloc] init];//最底部view
[backgroundView addSubview:bottomView];
[backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {//预设scrollView最底部控件约束!!!
make.edges.equalTo(self.view);
make.bottom.equalTo(bottomView.mas_top).offset(-103);
}];
// make.left.and.right.equalTo(self.view); 这里scrollView是垂直方向滚动,所以设置第一个子视图的左右约束于self.view(可以是别的view)的左右约束相等,切记不能设置成和scrollView的约束相等。!!!
// make.top.equalTo(self.scrollView); 最顶部的子视图的top约束和scrollView相等,切记不能设置为self.view,不然scrollView无法滚动。!!!
UIImageView*topbagImage = [[UIImageView alloc]init];
[backgroundView addSubview:topbagImage];
[topbagImage mas_makeConstraints:^(MASConstraintMaker *make) {//scrollview顶部控件约束 设置左右须是self.view
make.top.equalTo(backgroundView);
make.left.right.equalTo(self.view);
make.height.mas_equalTo(230);
}];
topbagImage.image= [UIImage imageNamed:@"bg_详情页"];
UIImageView*headView = [[UIImageView alloc]init];
[backgroundView addSubview:headView];
[headView mas_makeConstraints:^(MASConstraintMaker*make) {
make.bottom.equalTo(topbagImage);
make.centerX.equalTo(topbagImage);
make.width.height.mas_equalTo(66);
}];
headView.layer.cornerRadius=33;
headView.layer.masksToBounds=YES;
headView.image= [UIImage imageNamed:@"head"];
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {//设置最底部view 距离上一个子view的约束 left和right必须设置为self.view;!!!
make.top.equalTo(headView.mas_bottom).offset(800);
make.left.right.equalTo(self.view);
}];
}