从网易博客转战简书已经3个月了,还没提笔写过一篇文章,那就从今天开始写自己的第一篇文章吧,算是激励自己继续写作的动力,既然是开篇,那么就从iOS开发中容易混淆的三个概念开始吧,之前也看过一些其他作者写的此类文章,但是大都是用纯文字去描述这三者的区别,对于iOS初学者而言,不太容易理解,所以笔者就采用图解的方式去阐述它们之间的区别,若有不恰当之处,望读者批评指正,共同进步。
iOS中,UIScrollView是可以滚动的视图,其中最常用的UITableView就是继承了UIScrollView。
contentSize、contentInset和contentOffset 是scrollView三个基本的属性。
contentSize:即内容,就是scrollview可以滚动的区域,比如frame = (0 ,0 ,100 ,200) contentSize = (100 ,400),代表你的scrollview可以上下滚动,滚动区域为frame大小的两倍。其中常用的是contentSize.height = 内容的高度。
contentOffset:即偏移量,其中分为contentOffset.y=内容的顶部和frame顶部的差值,contentOffset.x=内容的左边和frame左边的差值,下面重点阐述contentOffset.y,因为contentOffset.y最为常用。
contentInset:即内边距,contentInset = 在内容周围增加的间距(粘着内容),contentInset的单位是UIEdgeInsets,默认值为UIEdgeInsetsZero。
说完了三者的定义,下面介绍contentSize到底包含哪几部分,tableView的内容(即contentSize)仅包含下面三部分:
tableView的内容(即contentSize):
1> cell
2> tableHeaderView\tableFooterView
3> sectionHeader\sectionFooter
其中
contentSize.height = 内容的高度
contentOffset.y = 内容的顶部 和 frame顶部 的差值
contentInset = 在内容周围增加的间距(粘着内容)
请读者牢记上面的概念介绍,对理解下面的情形有一定的帮助。
情形1:
•没有cell
•没有contentInset
•没有tableHeaderView\tableFooterView
![](https://img.haomeiwen.com/i1461605/a6f54e8d67054e55.png)
情形2:
•没有cell
•没有contentInset
•有tableHeaderView\tableFooterView
![](https://img.haomeiwen.com/i1461605/cfefc1dd2fd9de53.png)
情形3:
•有cell
•没有contentInset
•没有tableHeaderView\tableFooterView
![](https://img.haomeiwen.com/i1461605/55e317ff7260ccef.png)
情形4:
•有cell
•有contentInset
•没有tableHeaderView\tableFooterView
![](https://img.haomeiwen.com/i1461605/19b0613147c0bb97.png)
情形5:
•有cell
•没有contentInset
•有tableHeaderView\tableFooterView
![](https://img.haomeiwen.com/i1461605/9a06ed492877c6ab.png)
情形6:
•有cell
•有contentInset
•有tableHeaderView\tableFooterView
![](https://img.haomeiwen.com/i1461605/e0f55655357cad70.png)
情形7:
•有cell
•有contentInset
•没有tableHeaderView\tableFooterView
•添加了一个子控件,frame是CGRectMake(0, 0, 300, 50)
•子控件的frame以父控件内容左上角为坐标原点{0,0}
![](https://img.haomeiwen.com/i1461605/ce2dac9d4f680a6a.png)
![](https://img.haomeiwen.com/i1461605/7dcf23fd2bd947a8.png)
情形8:
•有cell
•有contentInset
•没有tableHeaderView\tableFooterView
•添加了一个子控件,frame是CGRectMake(0,-50, 300, 50)
•子控件的frame以父控件内容左上角为坐标原点{0,0}
![](https://img.haomeiwen.com/i1461605/464ba59588cef876.png)
![](https://img.haomeiwen.com/i1461605/6221ff48fcabefd6.png)
情形9:
•有cell
•没有contentInset
•有tableHeaderView\tableFooterView
•添加了一个子控件,frame是CGRectMake(0, 0, 300, 50)
![](https://img.haomeiwen.com/i1461605/05cbfe1f690d3909.png)
![](https://img.haomeiwen.com/i1461605/235cda285af18dbe.png)
网友评论
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.isTracking) {
if (fabsf(scrollView.contentOffset.y)>=30.0) {
UIEdgeInsets insets =self.tableView.contentInset;
insets.top=30;
self.tableView.contentInset=insets;
NSLog(@"=====%f",scrollView.contentOffset.y);
}
2017-11-29 11:04:35.551999+0800 jijiuh[8293:7764693] =====-28.500000
2017-11-29 11:04:35.788500+0800 jijiuh[8293:7764693] =====-29.000000
2017-11-29 11:04:35.835559+0800 jijiuh[8293:7764693] =====-29.500000
2017-11-29 11:04:35.855895+0800 jijiuh[8293:7764693] =====-30.000000
2017-11-29 11:04:35.901743+0800 jijiuh[8293:7764693] =====-45.000000 这里跳了
2017-11-29 11:04:35.990865+0800 jijiuh[8293:7764693] =====-45.500000
2017-11-29 11:04:36.080947+0800 jijiuh[8293:7764693] =====-46.000000
请问这是什么原因?
contentsize:内容显示的尺寸,包括tableheadview。 cell, tableFootView,而且tableview添加子视图的顶点是contentsize的左上角,但如果子视图的起点为-50,则contentsize自动会向上扩展50.
contentInset:附着内容尺寸,在contentsize的四周
contentOffset.y:以滚动的y值,从-contentinset.top 开始计算
不知道我的理解对不对,望多多指导!谢谢
inset.top唯一对contentOffset的影响就是.contentOffset.y的变化范围会受到影响.例如inset.top =0时候, contentOffset.y的范围就是(*, 0), 如果inset.top = 10的时候,contentOffset.y的范围会是(*, 10), *表示一个负数应该是tableview.height - content.height.
如果需要自定义下拉刷新控件, 那么RefreshControl的frame的x和height都会受到contentOffset.y和contentInset.top的影响