需求:UIView 设置背景图,并根据屏幕大小自动铺满缩放。
实现代码:
-(void)setUIViewBackgound:(UIView *)uiview name:(NSString *)name {
UIGraphicsBeginImageContext(uiview.frame.size);
[[UIImage imageNamed:name] drawInRect:uiview.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
uiview.backgroundColor = [UIColor colorWithPatternImage:image];
}
使用代码:
[self setUIViewBackgound:self.view name:@"main_bg.png"];
当然也可以将实现代码封装到工具类里面作为类方法使用。
参考网址:
http://stackoverflow.com/questions/8077740/how-to-fill-background-image-of-an-uiview