使用Masonry框架对控件设置约束后运行崩溃的解决办法

console报错信息

couldn't find a common superview for ; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}> and >



原因

不能为scrollView找到父视图,原因为当scrollView被创建后,使用masonry对scrollView进行设置约束(constrains)时会找视图进行参考,我们这里是以其父视图view进行参考,但此时scrollView并未添加到view中,所以运行会直接崩掉,解决办法为使添加到父控件的代码优先执行即可。

原代码:

scrollView.backgroundColor = [UIColor brownColor];    
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) 
{
       make.size.mas_equalTo(self.view);    
}];
[self.view addSubview:scrollView];```

####改正后:
```UIScrollView *scrollView = UIScrollView.new;
[self.view addSubview:scrollView];  //从尾部提高到此处
scrollView.backgroundColor = [UIColor brownColor];    
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) 
{
      make.size.mas_equalTo(self.view);
}];```

你可能感兴趣的:(使用Masonry框架对控件设置约束后运行崩溃的解决办法)