ScrollView 布局
提示
完整的 ScrollView 示例请参阅 英文版文档。
在 UIScrollView 中使用 Auto Layout 布局可能比较棘手,Masonry 让它变得简单。
基本设置
关键原则:ScrollView 的内容大小由其内容视图的约束决定。
::: code-group
// 固定 scrollView 到边缘
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// 创建内容视图
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.width.equalTo(scrollView); // 防止水平滚动
}];
scrollView.mas.makeConstraints { make in
make.edges.equalTo(view)
}
contentView.mas.makeConstraints { make in
make.edges.equalTo(scrollView)
make.width.equalTo(scrollView) // 防止水平滚动
}
:::
重要
确保内容视图的约束从上到下(垂直滚动)或从左到右(水平滚动)形成完整的链条。缺少最后一个约束会导致内容大小为零。