iOS开发之 背景平铺

来源:互联网 发布:数据眼txt qingkan 编辑:程序博客网 时间:2024/05/16 04:37

有的时候我们需要将一张图片平铺在View上当做背景。
以实现滚动页面时让背景也跟随移动。(例如苹果iBook首页背景的书架)

其实实现起来很简单。
代码如下:

//声明一个UIScrollView并改变他的ContentSize使其可以垂直滚动UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];[scrollView setContentSize:CGSizeMake(0, 2000)]; //声明一个背景图片UIImage *backgroundImage = [UIImage imageNamed:@"Background.png"]; //将刚刚生成的图片转换为UIColor对象。这样便可以实现平铺了。UIColor *backgroundColor = [UIColor colorWithPatternImage:backgroundImage]; //设置scrollView背景[scrollView setBackgroundColor:backgroundColor]; [self.view insertSubview:scrollView atIndex:0];


原创粉丝点击