ios:设置视图背景图片的方法

1. 使用一个UIImageView实例做子视图,并且放最后面
UIImageView *customBackgournd = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
self.background = customBackground;
[customBackground release];

[self addSubview:background];
[self sendSubViewToBack:background];

如果是用ib,就在xib文件中添加UIImageView,放在最下面。

2. 

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]];

推荐使用这种,方便。

今天在代码中使用

    self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"instrument_bg@2x.png"]];

结果导致instrument_bg@2x.png这张图片本来是640x960的,结果显示的时候只显示了左边一半出来与上面一半出来。

正确的代码应该是

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"instrument_bg"]];

这样的话,会自动去匹配图片。

错误的写法导致了图片放大了2倍,所以看起来就只有一半了。另外图片的后缀一般也省略为好

原文地址:https://www.cnblogs.com/langtianya/p/3911393.html