今天在写毕设的时候发现一个问题,那就是原来的所有子控件是加在view上的,但由于在真机上测试需要弹出键盘,所以这时候就需要把原来的view替换成UIScrollView,于是我新建了一个UIScrollView,并对它添加约束,到这里都非常顺利.
接下来坑就来了,我把所有的subview从原来的控制器view都加载到了UIScrollView上,一运行发现宽度不对,这个问题困扰了半天,有人说新建一个 Container,结果是无效的,.自己又捣鼓了半天,终于发现在container设置约束时,不光要设置上下左右,并且宽度和高度也要设置,此时UIScrollView的contentsize也会确定,如果不设置就会有各种问题.
UIScrollView * scrollView = [[UIScrollView alloc]init];
[self.view addSubview:scrollView];
scrollView.backgroundColor = [UIColor greenColor];
[scrollView makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(0);
make.top.equalTo(self.mas_topLayoutGuide).offset(0);
}];
NSLog(@"%@",NSStringFromCGRect(self.view.frame));
NSLog(@"%@",NSStringFromCGRect(scrollView.frame));
UIView * container = [[UIView alloc]init];
container.backgroundColor = [UIColor blueColor];
[scrollView addSubview:container];
[container makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalTo(0);
make.height.equalTo(1000);
make.width.equalTo(1000);
make.bottom.equalTo(scrollView.bottom).offset(0);