UIViewController背景图片
如何设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用:
Apple 高清图像指南
you could use:
Apple HD image Guide
self.view.layer.contents = (id)[UIImage imageNamed:@"background.png"].CGImage;
原因:每个视图都由 CALayer 类支持。根据 CALayer 上的文档:
是的,这个属性是可动画的。这意味着可以在这一层上完成基于 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:
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.
对此的快速解决方案...
A swift solution for this...
Swift 3.0 的解决方案
Solution for Swift 3.0
扩展 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.