Masonry适配——(4)UIScrollView的设置
2016-04-23 18:29
555 查看
UISCrollView在使用时,如果作为父视图包含多个子视图时,我们需要设置它的contentSize的大小,否则无法进行滚动显示。使用frame进行设置时,只需要令xxxScrollView.contentSize = CGSizeMake(width,height)即可,且不管是水平方向,还是垂直方向都方便设置。
而当使用masonry时,应该怎么进行设置呢?因为使用masonry时,无法通过CGSizeMake进行设置它的contentSize,所以需要通过一个过渡视图进行处理。即scrollView作为父视图,添加子视图,即过渡视图A,然后再将其他真正需要添加到scrollView上的子视图a、b、c等添加到过渡视图A中,然后再将a、b、c等中的最后一个子视图的右边距,或底边距设置成过渡视图A的右边距,或底边距,以此来设置scrollView的contentSize在水平方向,或垂直方向的大小。
效果如下图所示(水平方向、垂直方向)
代码示例:
水平方向
UIScrollView *horizontalScrollView = [[UIScrollViewalloc]
init];
horizontalScrollView.backgroundColor = [UIColororangeColor];
horizontalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父视图,并设置其约束
[self.viewaddSubview:horizontalScrollView];
[horizontalScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.left.mas_equalTo(10);
make.right.mas_equalTo(-10);
make.height.mas_equalTo(100);
}];
// 设置scrollView的子视图,即过渡视图contentSize,并设置其约束
UIView *horizontalContainerView = [[UIViewalloc]
init];
[horizontalScrollView addSubview:horizontalContainerView];
[horizontalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(horizontalScrollView);
make.height.equalTo(horizontalScrollView);
}];
//
过渡视图添加子视图
UIView *previousView =nil;
for (int i =0; i <
10; i++)
{
UILabel *label = [[UILabelalloc]
init];
label.textAlignment =NSTextAlignmentCenter;
label.backgroundColor = [UIColorcolorWithHue:(arc4random()
%256 / 256.0)
saturation:(arc4random() %128 /
256.0) +0.5
brightness:(arc4random() %128 /
256.0) +0.5
alpha:1];
label.text = [NSStringstringWithFormat:@"第
%d个视图", i];
//
添加到父视图,并设置过渡视图中子视图的约束
[horizontalContainerView
addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.bottom.equalTo(horizontalContainerView);
make.width.equalTo(horizontalScrollView);
if (previousView)
{
make.left.mas_equalTo(previousView.mas_right);
}
else
{
make.left.mas_equalTo(0);
}
}];
previousView = label;
}
// 设置过渡视图的右距(此设置将影响到scrollView的contentSize)
[horizontalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(previousView.mas_right);
}];
垂直方向
UIScrollView *verticalScrollView = [[UIScrollViewalloc]
init];
verticalScrollView.backgroundColor = [UIColorgreenColor];
verticalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父视图,并设置其约束
[self.viewaddSubview:verticalScrollView];
[verticalScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.bottom.and.right.mas_equalTo(-10.0);
make.height.mas_equalTo(100);
}];
// 设置scrollView的子视图,即过渡视图contentSize,并设置其约束
UIView *verticalContainerView = [[UIViewalloc]
init];
[verticalScrollView addSubview:verticalContainerView];
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.and.right.equalTo(verticalScrollView).with.insets(UIEdgeInsetsZero);
make.width.equalTo(verticalScrollView);
}];
//
过渡视图添加子视图
UIView *lastView =nil;
for (NSInteger index =0; index <
10; index++)
{
UILabel *label = [[UILabelalloc]
init];
label.textAlignment =NSTextAlignmentCenter;
label.backgroundColor = [UIColorcolorWithHue:(arc4random()
%256 / 256.0)
saturation:(arc4random() %128 /
256.0) +0.5
brightness:(arc4random() %128 /
256.0) +0.5
alpha:1];
label.text = [NSStringstringWithFormat:@"第
%ld个视图", index];
//
添加到父视图,并设置过渡视图中子视图的约束
[verticalContainerView
addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(verticalContainerView);
make.height.mas_equalTo(verticalScrollView.mas_height);
if (lastView)
{
make.top.mas_equalTo(lastView.mas_bottom);
}
else
{
make.top.mas_equalTo(0);
}
}];
lastView = label;
}
// 设置过渡视图的底边距(此设置将影响到scrollView的contentSize)
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
而当使用masonry时,应该怎么进行设置呢?因为使用masonry时,无法通过CGSizeMake进行设置它的contentSize,所以需要通过一个过渡视图进行处理。即scrollView作为父视图,添加子视图,即过渡视图A,然后再将其他真正需要添加到scrollView上的子视图a、b、c等添加到过渡视图A中,然后再将a、b、c等中的最后一个子视图的右边距,或底边距设置成过渡视图A的右边距,或底边距,以此来设置scrollView的contentSize在水平方向,或垂直方向的大小。
效果如下图所示(水平方向、垂直方向)
代码示例:
水平方向
UIScrollView *horizontalScrollView = [[UIScrollViewalloc]
init];
horizontalScrollView.backgroundColor = [UIColororangeColor];
horizontalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父视图,并设置其约束
[self.viewaddSubview:horizontalScrollView];
[horizontalScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.left.mas_equalTo(10);
make.right.mas_equalTo(-10);
make.height.mas_equalTo(100);
}];
// 设置scrollView的子视图,即过渡视图contentSize,并设置其约束
UIView *horizontalContainerView = [[UIViewalloc]
init];
[horizontalScrollView addSubview:horizontalContainerView];
[horizontalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(horizontalScrollView);
make.height.equalTo(horizontalScrollView);
}];
//
过渡视图添加子视图
UIView *previousView =nil;
for (int i =0; i <
10; i++)
{
UILabel *label = [[UILabelalloc]
init];
label.textAlignment =NSTextAlignmentCenter;
label.backgroundColor = [UIColorcolorWithHue:(arc4random()
%256 / 256.0)
saturation:(arc4random() %128 /
256.0) +0.5
brightness:(arc4random() %128 /
256.0) +0.5
alpha:1];
label.text = [NSStringstringWithFormat:@"第
%d个视图", i];
//
添加到父视图,并设置过渡视图中子视图的约束
[horizontalContainerView
addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.bottom.equalTo(horizontalContainerView);
make.width.equalTo(horizontalScrollView);
if (previousView)
{
make.left.mas_equalTo(previousView.mas_right);
}
else
{
make.left.mas_equalTo(0);
}
}];
previousView = label;
}
// 设置过渡视图的右距(此设置将影响到scrollView的contentSize)
[horizontalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(previousView.mas_right);
}];
垂直方向
UIScrollView *verticalScrollView = [[UIScrollViewalloc]
init];
verticalScrollView.backgroundColor = [UIColorgreenColor];
verticalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父视图,并设置其约束
[self.viewaddSubview:verticalScrollView];
[verticalScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.bottom.and.right.mas_equalTo(-10.0);
make.height.mas_equalTo(100);
}];
// 设置scrollView的子视图,即过渡视图contentSize,并设置其约束
UIView *verticalContainerView = [[UIViewalloc]
init];
[verticalScrollView addSubview:verticalContainerView];
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.and.right.equalTo(verticalScrollView).with.insets(UIEdgeInsetsZero);
make.width.equalTo(verticalScrollView);
}];
//
过渡视图添加子视图
UIView *lastView =nil;
for (NSInteger index =0; index <
10; index++)
{
UILabel *label = [[UILabelalloc]
init];
label.textAlignment =NSTextAlignmentCenter;
label.backgroundColor = [UIColorcolorWithHue:(arc4random()
%256 / 256.0)
saturation:(arc4random() %128 /
256.0) +0.5
brightness:(arc4random() %128 /
256.0) +0.5
alpha:1];
label.text = [NSStringstringWithFormat:@"第
%ld个视图", index];
//
添加到父视图,并设置过渡视图中子视图的约束
[verticalContainerView
addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(verticalContainerView);
make.height.mas_equalTo(verticalScrollView.mas_height);
if (lastView)
{
make.top.mas_equalTo(lastView.mas_bottom);
}
else
{
make.top.mas_equalTo(0);
}
}];
lastView = label;
}
// 设置过渡视图的底边距(此设置将影响到scrollView的contentSize)
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
相关文章推荐
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(五)
- 异步消息处理机制
- easyui-textbox添加validType使maxlength失效
- ios UITableView实现单击提示,滑动删除,多选行等操作
- UI控件--桌面小组件(1)
- androidStudio出现java.exe finished with non-zero eit value 1
- 使用TexturePackerGUI配合NGUI打包图集
- Vue 过渡
- LeetCode *** 60. Permutation Sequence
- 1012 Rescue
- Vue 表单控件绑定
- Rebuild Future
- Vue 方法与事件处理器
- 对map根据value进行排序
- HTML标签marquee实现滚动效果
- Vue 列表渲染
- Vue 条件渲染
- UISwitch和UIActivity的使用
- Vue Class 与 Style 绑定
- iOS UITableViewCell 重用及 性能优化