contentsize、contentoffset 和contentInset 详解

下面我用怎么做一个书架的例子和大家说说contentsize、contentoffset 和contentInset的区别。

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)开始显示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值