如何设置UIViewController的背景图像?这样做的代码是什么?现在我有
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.jpg"]];
但我不想要一个模式。我希望它能正确填满屏幕。
答案 0 :(得分:34)
你可以使用:
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Blah.png"]];
[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];
[backgroundImage release];
答案 1 :(得分:5)
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的漂亮转换。 注意:如果支持多个方向,请小心。
答案 2 :(得分:4)
快速解决这个问题......
let backgroundImageView = UIImageView(image: UIImage(named: "backgroundImage"))
backgroundImageView.frame = view.frame
backgroundImageView.contentMode = .ScaleAspectFill
view.addSubview(backgroundImageView)
view.sendSubviewToBack(backgroundImageView)
答案 3 :(得分:1)
要扩展Jesse Naugher的代码:UIView(或UIViewController,就此而言),没有内置的方法将图像用作背景。您需要做的是使用UIImageView,它是一个专门用于显示图像的UIView子类,并将其添加到视图层次结构的后面。
答案 4 :(得分:1)
Swift 3.0的解决方案
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "home02.png")!)