简体   繁体   English

用背景图像覆盖UIView

[英]Overlaying UIView with an background image

Is it possible to overlay UIView with an image without using UIImageView from the Interface Builder? 是否可以在不使用Interface Builder的UIImageView情况下将UIView与图像叠加?

If not, how would you accomplish the same in code? 如果没有,你将如何在代码中完成相同的工作?

From the UIView documentation: 从UIView文档:

Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. 基于图像的背景 - 对于显示相对静态内容的视图,请考虑使用带手势识别器的UIImageView对象,而不是自己子类化和绘制图像。 Alternatively, you can also use a generic UIView object and assign your image as the content of the view's CALayer object. 或者,您也可以使用通用UIView对象并将图像指定为视图的CALayer对象的内容。

Using the second option, to set someView to have a background of yourImage.png : 使用第二个选项,设置someViewyourImage.png的背景:

someView.layer.contents = (id)[[UIImage imageNamed:@"yourImage.png"] CGImage];

if by Overlying a UIView you mean painting its background with an image, it can be done like this: 如果通过覆盖UIView你的意思是用图像绘制它的背景,它可以这样做:

someView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];

You could also add a new UIImageView that covers your UIView by code, Init your UIImageView with the same frame Rect as your UIView and add it as a subview. 您还可以通过代码添加一个覆盖UIView的新UIImageView,使用与UIView相同的Rect Rect初始化您的UIImageView并将其添加为子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM