UIScrollView
是個(gè)非常特殊的view
钥勋,UIScrollView
與其subview
之間相對(duì)位置的約束,并不會(huì)直接用于frame
的計(jì)算,而是會(huì)轉(zhuǎn)化為對(duì)ContentSize
的計(jì)算追驴。換句話說必盖,當(dāng)UIScrollView
知道了上下左右的約束分別指向subview
什么位置之后朵栖,只要subview
的位置固定下來了,ContentSize
的大小就確定下來了。
@interface Example1Controller ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *containView;
@end
@implementation Example1Controller
- (void)viewDidLoad {
[super viewDidLoad];
[self addPageSubviews];
[self layoutPageSubviews];
}
- (void)addPageSubviews {
[self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.containView];
[self.containView addSubview:self.topView];
[self.containView addSubview:self.bottomView];
}
- (void)layoutPageSubviews {
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.width.mas_equalTo(300);
make.height.mas_equalTo(300);
}];
[self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView);
make.width.equalTo(self.scrollView);
// make.height.equalTo(self.scrollView).multipliedBy(1.5);
}];
[self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.containView);
make.leading.equalTo(self.containView);
make.trailing.equalTo(self.containView);
make.height.mas_equalTo(200);
}];
[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.topView.mas_bottom);
make.leading.equalTo(self.containView);
make.trailing.equalTo(self.containView);
make.height.mas_equalTo(200);
make.bottom.equalTo(self.containView);
}];
}
#pragma mark - getter & setter
-(UIView *)containView {
if (!_containView) {
_containView = [[UIView alloc] init];
_containView.backgroundColor = [UIColor blueColor];
}
return _containView;
}
-(UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] init];
_scrollView.backgroundColor = [UIColor redColor];
}
return _scrollView;
}
- (UIView *)topView {
if (!_topView) {
_topView = [[UIView alloc] init];
_topView.backgroundColor = [UIColor grayColor];
}
return _topView;
}
-(UIView *)bottomView {
if (!_bottomView) {
_bottomView = [[UIView alloc] init];
_bottomView.backgroundColor = [UIColor yellowColor];
}
return _bottomView;
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者