很多時(shí)候,我們需要使用ScrollView來作為底層View,來使整個(gè)界面能夠滑動(dòng)顯示.使用Frame來設(shè)置各控件的坐標(biāo)時(shí),很簡(jiǎn)單,直接用ScrollView的ContentSize屬性就可以設(shè)置其滑動(dòng)范圍,但是使用Masonry的時(shí)候 ,這個(gè)方法就不行了,此時(shí),我們需要給ScrollView上加一層containerView,將各個(gè)控件都加在containerView上,然后根據(jù)最后一個(gè)控件的的位置來設(shè)置containerView的底部約束就可以了.廢話不多說,貼一下代碼:
1.先創(chuàng)建一個(gè)ScrollView
self.baseView = [[UIScrollView alloc] init];
self.baseView.backgroundColor = [UIColor clearColor];
self.baseView.delegate = self;
self.baseView.showsVerticalScrollIndicator = NO;
[self.view addSubview:self.baseView];
[self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
2.在ScrollView上添加一個(gè)containerView
UIView *containerView = [UIView new];
[self.baseView addSubview:containerView];
[containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.baseView);
make.width.mas_equalTo(self.baseView);
}];
3.將需要展示的控件添加在containerView上(此處只是示例代碼)
UIView *segementView1 = [[UIView alloc] init];
segementView1.backgroundColor = COLOR_BACKVIEW_GRAY;
[containerView addSubview:segementView1];
[segementView1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(containerView.mas_top).offset(topHeight);
make.left.mas_equalTo(containerView.mas_left).offset(0);
make.right.mas_equalTo(containerView.mas_right).offset(0);
make.height.mas_equalTo(8);
}];
UILabel *paytimeTipLab = [[UILabel alloc] init];
paytimeTipLab.textColor = COLOR_FONT_BLACK;
paytimeTipLab.text = @"支付剩余時(shí)間";
paytimeTipLab.textAlignment = NSTextAlignmentCenter;
paytimeTipLab.font = FONT_12;
[containerView addSubview:paytimeTipLab];
[paytimeTipLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(segementView1.mas_bottom).offset(34);
make.left.mas_equalTo(containerView.mas_left).offset(0);
make.right.mas_equalTo(containerView.mas_right).offset(0);
make.height.mas_equalTo(20);
}];
UILabel *paytimeLab = [[UILabel alloc] init];
paytimeLab.textAlignment = NSTextAlignmentCenter;
paytimeLab.textColor = COLOR_FONT_GRAY;
paytimeLab.font = FONT_20;
paytimeLab.text = format_time;
[containerView addSubview:paytimeLab];
self.paytimeLab = paytimeLab;
[paytimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(paytimeTipLab.mas_bottom).offset(20);
make.left.mas_equalTo(containerView.mas_left).offset(0);
make.right.mas_equalTo(containerView.mas_right).offset(0);
make.height.mas_equalTo(30);
}];
UIView *segementView2 = [[UIView alloc] init];
segementView2.backgroundColor = COLOR_BACKVIEW_GRAY;
[containerView addSubview:segementView2];
[segementView2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(paytimeLab.mas_bottom).offset(34);
make.left.mas_equalTo(containerView.mas_left).offset(0);
make.right.mas_equalTo(containerView.mas_right).offset(0);
make.height.mas_equalTo(8);
}];
4.最后一步,設(shè)置一下containerView的底部約束即可(這里放最后一個(gè)控件的底部)
[containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(segementView2.mas_bottom).offset(200);
}];
好了,這樣就可以確定ScrollView的滑動(dòng)范圍了,希望可以給初始使用Masonry的同學(xué)一些幫助.測(cè)試的時(shí)候可以在containerView上多添加一些控件,這樣會(huì)效果會(huì)更直觀.