UIScrollView的AutoLayout约束
首先UIScrollview包含自身的frame和contentSize二个部分。frame决定其展示给用户的可见区域,contentSize决定其整个内容的大小。如果frame的宽高小于contentSize,则UIScrollview可以在相应的方向滚动。UIScrollView中有个contentSize属性,如果内容大小是固定的,你可以直接设置contentSize。但是在自动布局约束中我们往往不知道其具体的大小,或者不想计算其大小,在这种情况下我们怎么实现UIScrollView的约束呢?
关于UIScroolView的AutoLayout约束,首先得明白一点,UIScrollView上面的subviews与UIScrollView的top,bottom,left、right约束都是相对于UIScrollview的contentsize,而contentsize大小是不确定的,为了明白的说明这一点,我们看下面代码:
UIScrollView *verticalScrollView = [[UIScrollView alloc]init];
verticalScrollView.backgroundColor = [UIColor greenColor];
verticalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父视图,并设置其约束
[self.view addSubview:verticalScrollView];
[verticalScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.right.mas_equalTo(-10.0);
make.centerY.equalTo(0);
make.height.mas_equalTo(100);
}];
// 设置scrollView的子视图,即过渡视图contentSize,并设置其约束
UIView *verticalContainerView = [[UIView alloc]init];
[verticalScrollView addSubview:verticalContainerView];
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.and.right.equalTo(verticalScrollView).with.insets(UIEdgeInsetsZero);
}];
verticalContainerView.backgroundColor = [UIColor redColor];
您觉得最后在屏幕上的verticalScrollView显示的是什么颜色呢?我们先来看答案!

你没眼花,就是绿色的。具体原因是verticalContainerView是verticalScrollView上的一个subView,其相对于verticalScrollView的top,bottom,left、right的约束都是相对于verticalScrollView的contentSize,而contentSize的默认大小为CGSizeZero,因此红色verticalContainerView是完全不能显示的。那么我们怎么让红色的部分显示出来?很简单,我们只需重新约束verticalContainerView,具体代码如下:
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.and.right.equalTo(verticalScrollView).with.insets(UIEdgeInsetsZero);
make.width.equalTo(verticalScrollView);
make.height.equalTo(50);
}];
运行结果:

当然,在实际编程中,这样约束,并没有什么实际意义,既然我大小都知道了,直接用frame和contentSize就可以了。其强大性体现在其contentSize会根据verticalContainerView的大小变化而变化,因此我可以在verticalContainerView增加很多subViews,用subViews的内容大小扩充contentSize。具体可见如下的垂直滚动的UIScroolView代码:
UIScrollView *verticalScrollView = [[UIScrollView alloc]init];
verticalScrollView.backgroundColor = [UIColor greenColor];
verticalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父视图,并设置其约束
[self.view addSubview:verticalScrollView];
[verticalScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.right.mas_equalTo(-10.0);
make.centerY.equalTo(0);
make.height.mas_equalTo(100);
}];
// 设置scrollView的子视图,即过渡视图contentSize,并设置其约束
UIView *verticalContainerView = [[UIView alloc]init];
[verticalScrollView addSubview:verticalContainerView];
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.and.right.equalTo(verticalScrollView).with.insets(UIEdgeInsetsZero); make.width.equalTo(verticalScrollView);
}];
verticalContainerView.backgroundColor = [UIColor redColor]; UIView *lastView =nil;
for (NSInteger index =0; index <8; index++)
{
UILabel *label = [[UILabel alloc]init];
label.textAlignment =NSTextAlignmentCenter;
label.backgroundColor = [UIColor colorWithHue:(arc4random() %256 / 256.0)
saturation:(arc4random() %128 /256.0) +0.5
brightness:(arc4random() %128 /256.0) +0.5
alpha:1];
label.text = [NSString stringWithFormat:@"第 %ld个视图", index]; //添加到父视图,并设置过渡视图中子视图的约束
[verticalContainerView addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(verticalContainerView);
make.height.mas_equalTo(60); if (lastView)
{
make.top.mas_equalTo(lastView.mas_bottom);
}
else
{
make.top.mas_equalTo(0);
}
}]; lastView = label;
} [verticalContainerView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
UIScrollView的AutoLayout约束的更多相关文章
- 利用代码添加autolayout约束
1.概述 通常我们通过storyboard能够完成的,代码也能够完成,所以这里介绍下代码实现约束的添加,通常我们不这么干(在不使用第三方框架的情况下,使用系统自带的类添加约束特别繁琐),所以这里仅仅简 ...
- 使用代码创建AutoLayout约束
使用代码创建AutoLayout约束 1.代码创建约束的步骤 2.代码创建约束的常用方法 3.代码创建约束的原则 4.禁用Autoresizing的原因 5. 设置相对状态栏的约束,使用self.to ...
- 示例详解:UIScrollview 与 Autolayout 的那点事
前言 自从写了介绍Masonry那篇文章以后 就一直有人对UIScrollView的那个例子不是很理解 UIView *container = [UIView new]; [scrollView ad ...
- UIScrollview 与 Autolayout 的那点事
原文 http://www.cocoachina.com/ios/20151221/14757.html 前言 自从写了 介绍Masonry 那篇文章以后 就一直有人对UIScrollView的那个 ...
- iOS - UIScrollView xib添加约束的坑
一.Storyboard中的UIScrollView使用自动布局 在使用storyboard和xib时,我们经常要用到ScrollView,还有自动布局AutoLayout,但是ScrollView和 ...
- UIScrollView在AutoLayout下的滚动问题
使用Storyboard编写UI,设置支持AutoLayout. 在其中的一个场景上,添加一个UIScrollView,在对应的代码里增加 - (void)viewDidLoad { [super v ...
- AutoLayout 约束与frame、bounds的设置
介绍 关于AutoLayout的介绍可参考: 使用解读: https://segmentfault.com/a/1190000004386278 iOS 屏幕适配,autoResizing autoL ...
- UIScrollView使用autolayout 垂直滚动
转自:http://dadage456.blog.163.com/blog/static/30310744201491141752716 1.创建一个空白的UIViewController .将UIS ...
- Autolayout约束动画化-Animating Autolayout Constraints
原文:Animating Autolayout Constraints 作者:@kharrison 译者:CocoaChina--起个名字好难(CC论坛ID) 首发:CocoaChina 记于二零一五 ...
随机推荐
- springboot配置文件(.yml)中自定义属性值并在controller里面获取
1,由于项目需要,学习了新的框架--springboot,顺便练习一下在.yml中配置自定义属性并在controller里面获取.(以下的Springboot框架我已经搭建好,就不在陈述) 2,spr ...
- 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41771 Accepted: 16955 De ...
- 模糊查询出list
<resultMap id="shipList" type="com.sinochem.yunlian.yunliantraffic.domain.Ship&quo ...
- JDBC 链接mysql 8 的问题
转载:jdbc连接mysql 8 的一些坑 1.驱动包要升级为 mysql-connector-java-8.0.11.jar 2.JDBC driver 由“com.mysql.jdbc.Drive ...
- css常用选择器选择器
tap选择器 ulclass选择器 .id选择器 #后代选择器 a b子代选择器 a>b兄弟选择器 a + b 以a为参考给b加样式属性选择器 input [type="text&qu ...
- CentOS官网下载系统镜像
https://jingyan.baidu.com/article/1876c85279cedd890a13766c.html
- python爬虫之常见的加密方式
前言 数据加密与解密通常是为了保证数据在传输过程中的安全性,自古以来就一直存在,古代主要应用在战争领域,战争中会有很多情报信息要传递,这些重要的信息都会经过加密,在发送到对应的人手上. 现代 ,在网络 ...
- Python之字符串函数str()
str()方法使Python将非字符串值表示为字符串: age = 23 message = "Happy" + str(age) +"rd Birthday"
- java反射2
package com.wen; import java.lang.reflect.Field;import java.lang.reflect.Method; public class Test2 ...
- Servlet中的编码问题
对于response.setContentType()和response.setCharacterEncoding()的理解: 经过一些实践,对着两个方法有了一些自己的理解,有可能今后的学习中会发现自 ...