- 建立UIScrollView窗口,我们只打算用手机的上半屏显示图像,(这一步也可以在storyboard里完成)
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320 , 300)];
2.建立内容视图
UIImageView * view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tesla.jpg"]];
3.将内容视图作为scrollView的子视图
[myScrollView addSubview: view];
4.当然了,还得把scrollView添加到视图结构中
[self.view addSubview: myScrollView];
5.设置滚动的内容尺寸
myScrollView.contentSize = CGSizeMake(imageW, imageH);
myScrollView.showsHorizontalScrollIndicator = NO;//不显示水平拖地的条
myScrollView.showsVerticalScrollIndicator=NO;//不显示垂直拖动的条
myScrollView.pagingEnabled = YES;//允许分页滑动
myScrollView.bounces = NO;//到边了就不能再拖地
注意事项:
UIScrollView被push之后返回,会发生控件位置偏移,用下面的代码就OK
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;
网友评论