iOS 开发整理

UIView设置背景色图片及填充

2018-04-14  本文已影响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; 
上一篇下一篇

猜你喜欢

热点阅读