UIViewController背景图片

发布于 2024-09-14 09:50:11 字数 204 浏览 3 评论 0原文

如何设置 UIViewController 的背景图像?执行此操作的代码是什么?现在我有

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

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

How do I set the background image of my UIViewController? What is the code to do it? Right now I have

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

but I don't want a pattern. I want it to fill the screen properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清风夜微凉 2024-09-21 09:50:11

您可以使用:

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

[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];
[backgroundImage release];

Apple 高清图像指南

you could use:

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

[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];
[backgroundImage release];

Apple HD image Guide

零時差 2024-09-21 09:50:11

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 的良好过渡。
注意:如果您支持多个方向,请小心。

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

Reason: each view is backed by a CALayer class. According to documentation on 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.

And yes, this property is animatable. This means that nice transitions based on CoreAnimation can be done on this layer.
Note: be careful if you support multiple orientations.

萌吟 2024-09-21 09:50:11

对此的快速解决方案...

let backgroundImageView = UIImageView(image: UIImage(named: "backgroundImage"))
backgroundImageView.frame = view.frame
backgroundImageView.contentMode = .ScaleAspectFill
view.addSubview(backgroundImageView)
view.sendSubviewToBack(backgroundImageView)

A swift solution for this...

let backgroundImageView = UIImageView(image: UIImage(named: "backgroundImage"))
backgroundImageView.frame = view.frame
backgroundImageView.contentMode = .ScaleAspectFill
view.addSubview(backgroundImageView)
view.sendSubviewToBack(backgroundImageView)
情何以堪。 2024-09-21 09:50:11

Swift 3.0 的解决方案

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "home02.png")!)

Solution for Swift 3.0

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "home02.png")!)
云淡风轻 2024-09-21 09:50:11

扩展 Jesse Naugher 的代码:UIView(或 UIViewController,就此而言)没有使用图像作为背景的内置方法。您所要做的就是使用 UIImageView(它是专门用于显示图像的 UIView 子类),并将其添加到视图层次结构的后面。

To expand on Jesse Naugher's code: UIView (or UIViewController, for that matter), holds no built-in way of using an image for background. What you have to do is to use a UIImageView, which is a UIView subclass specifically for displaying images, and add that to the back of your view hierarchy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文