您的位置:首页 > 移动开发 > IOS开发

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

2014-08-13 23:02 621 查看
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倍,所以看起来就只有一半了。另外图片的后缀一般也省略为好
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: