1、设置平铺背景
self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];
2、设置拉伸背景
UIImage *oldImage = [UIImage imageNamed:@"me"];
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
[oldImage drawInRect:self.view.bounds];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];
3、设置条纹背景
//view :传入VIEW viewBgColor:View背景色 rowHeight:条纹高度
//lineWidth :底端线条粗细 margin:距离传入VIEW左右间隙
-(void)setBgStyleWithView:(UIView *)view viewBgColor:(UIColor *)bgColor rowHeight:(CGFloat)rowHeight lineWidth:(CGFloat )lineWidth margin:(CGFloat )margin{
//可以改造 把view 设置为属性 self.yourview 替换掉 view
//背景色
view.backgroundColor = bgColor;
// 1.创建一行背景图片
CGFloat rowW = self.view.frame.size.width;
CGFloat rowH = rowHeight;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 画矩形框
[[UIColor redColor] set];
CGContextAddRect(ctx, CGRectMake(0, 0, rowW, rowH));
CGContextFillPath(ctx);
// 2.画线
[[UIColor greenColor] set];
CGContextSetLineWidth(ctx, lineWidth);
CGFloat dividerX = margin;
CGFloat dividerY = rowH - lineWidth;
CGContextMoveToPoint(ctx, dividerX, dividerY);
CGContextAddLineToPoint(ctx, rowW - dividerX, dividerY);
CGContextStrokePath(ctx);
// 3.取图
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 4.结束上下文
UIGraphicsEndImageContext();
// 5.设置为背景
view.backgroundColor = [UIColor colorWithPatternImage:newImage];
}
4、例图