UIViewController背景图片

Cod*_*Guy 13 iphone background-image uiviewcontroller

如何设置UIViewController的背景图像?这样做的代码是什么?现在我有

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.jpg"]];
Run Code Online (Sandbox Code Playgroud)

但我不想要一个模式.我希望它能正确填满屏幕.

Jes*_*her 34

你可以使用:

UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Blah.png"]];

[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];
[backgroundImage release];
Run Code Online (Sandbox Code Playgroud)

Apple高清图像指南


vig*_*o24 6

self.view.layer.contents =(id)[UIImage imageNamed:@"background.png"].CGImage;

原因:每个视图都由CALayer类支持.根据CALayer的文件:

contents
An object that provides the contents of the layer. Animatable.


@property(retain) id contents

Discussion
A layer can set this property to a CGImageRef to display the image as its contents. The default value is nil.

是的,这个属性是可动画的.这意味着可以在此层上完成基于CoreAnimation的漂亮转换.注意:如果支持多个方向,请务必小心.