美文网首页iOS 开发整理
UIView设置背景色图片及填充

UIView设置背景色图片及填充

作者: 流星载梦 | 来源:发表于2018-04-14 10:01 被阅读9次

关于设置UIView的背景为图片的方法及问题

1.加一个uiimageview在uiview上面,图片会自动拉伸到屏幕view的大小

UIImageView* imageView = [[UIImageView alloc] initWithFrame:view.bounds];      
imageView.image = [[UIImage imageNamed:@"name.png"] stretchableImageWithLeftCapWidth:left topCapHeight:top];      
[view addSubview:imageView];  

2,图片会自动拉伸到屏幕view的大小,图片不会拉伸到View的大小,如果图片尺寸小于View大小会平铺

1.imageNamed方式
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"name.png"]];  

2.contentOfFile方式
NSString* path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"png"];      
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path];

3.quartzCore方式,图片会自动拉伸到屏幕view的大小

UIImage *image = [UIImage imageNamed:@"name.png"];      
view.layer.contents = (id) image.CGImage;    // 如果需要背景透明加上下面这句      
view.layer.backgroundColor = [UIColor clearColor].CGColor; 

相关文章

网友评论

    本文标题:UIView设置背景色图片及填充

    本文链接:https://www.haomeiwen.com/subject/gbikkftx.html