下面我用怎么做一个书架的例子和大家说说contentsize、contentoffset 和contentInset的区别。
[plain]
NSString *path = nil;
UIImage *back = nil;
NSString *str = nil;
UIImage *aImage = nil;
path = [[NSBundle mainBundle] pathForResource:@"shujia" ofType:@"jpg"];
back = [UIImage imageWithContentsOfFile:path];//加载图片
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(96, 84,back.size.width,back.size.height)];
_scrollView.showsVerticalScrollIndicator = NO; //将竖直方向的滚动条隐藏
//将shujia.jpg作为_scrollerView的背景
UIColor *color=[[UIColor alloc]initWithPatternImage:back];
[_scrollView setBackgroundColor:color];
_buttonList=[NSMutableArray arrayWithCapacity:9];
for(int i=0;i<9;i++)
{
str=[NSString stringWithFormat:@"menubook%d",i+1];
path = [[NSBundle mainBundle] pathForResource:str ofType:@"png"];
aImage = [UIImage imageWithContentsOfFile:path];
int x = (i%3)* (aImage.size.width+15);
int y = (i/3)* (aImage.size.height+30);
_whichPeople = [UIButton buttonWithType:UIButtonTypeCustom];
_whichPeople.frame = CGRectMake(x+10,y+30,aImage.size.width,aImage.size.height);
_whichPeople.tag = i;
[_whichPeople setImage:aImage forState:UIControlStateNormal];//这个图片是在布点击时响应
path = nil;
aImage = nil;
str = nil;
str=[NSString stringWithFormat:@"menubook%d_1",i+1];
path = [[NSBundle mainBundle] pathForResource:str ofType:@"png"];
aImage = [UIImage imageWithContentsOfFile:path];
[_whichPeople setImage:aImage forState:UIControlStateHighlighted];//图片在点击的时候响应
[_whichPeople addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:_whichPeople];
[_buttonList addObject:_whichPeople];
path = nil;
aImage = nil;
str = nil;
}
//控制可以滚动的区域
_scrollView.contentSize = CGSizeMake(480, 580);
//当前显示区域顶点相对于frame顶点的偏移量
_scrollView.contentOffset = CGPointMake(0, 0);
//scrollview的contentview的顶点相对于scrollview的位置
_scrollView.contentInset = UIEdgeInsetsMake(0 , 0, 0, 0);
[self.view addSubview:_scrollView];
contentSize是scrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下滚动,滚动区域为frame大小的两倍。
contentOffset是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了480
contentInset是scrollview的contentview的顶点相对于scrollview的位置,例如你的contentInset = (0 ,100),那么你的contentview就是从scrollview的(0 ,100)开始显示
这几个都是在ios程序中,经常会注意到的一些小细节,能否真正了解这些,对写ios程序也有很大的好处。
frame 是UIView中表示此view的一个矩形面积,包括了view在它的superview中的一些几何上的标识。有起始坐标,也就是origin(CGPoint)和这个矩形面积的宽(width)和高(height)
bounds 每个view,除了有对应它的superview的相关坐标外,它自己也有对应它自己的几何坐标,所以bounds就是标识它自己坐标关系的一个标识,它有和frame一样的属性,但在
数值上会有些不同。
center 是一个view的中心,默认是这个view对应superview坐标的中间值。
如下图
假如有个view-A ,它是view-B的一个subview,view-A在view-B上的起始坐标是(40,40)(此坐标是view-B上的坐标关系),view-A的高(height)为380,宽(width)为240.
所以,view-A的frame值是(40,40,240,380) bounds的值是(0,0,240,380) center的值是(160,230). 所以,frame和bounds是有关联的,修改其中一个的宽或高,另一个也会相应的改变。
contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个scrollview,它的frame为(0,0,320,480),而它的contentSize为(320,960).也就是说,这个scrollview整个内容的大小为(320,960),要通过上下滑动scrollview来查看(320,480)后的内容。
contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了480
contentInset 是scrollview中contentView.frame.origin与scrollview.frame.origin的关系,比如contentView的frame为(0,30,320,480),那么contentInset则为(0, 30),
- contentInset应该是4个参数(top,left,bottom,right)你这个上面怎么是(0,30)?应该是(30,0,0,0)才对 吧
- 对于contentSize ,说得有点不对,应该是frame顶点相对于scrollview当前显示区域顶点的偏移量,因为往下拉,y便宜的是-48 0
- 引自http://www.2cto.com/kf/201205/131760.html
-
http://blog.csdn.net/kylinbl/article/details/7043936