简体   繁体   English

在Interface Builder中添加背景图像

[英]Adding A Background Image In Interface Builder

I'm trying to add a custom image as a background for my application using Xcode 4.2, but I'm not too sure how to do it. 我正在尝试使用Xcode 4.2为我的应用程序添加自定义图像作为背景,但是我不太确定该怎么做。 I can add plain colors as the background in Interface Builder, but I do not see the option to add a custom image. 我可以在Interface Builder中添加纯色作为背景,但是看不到添加自定义图像的选项。 I have Googled this issue and have researched it for hours, but none of the solutions have seemed to work for me and are out of date. 我已经搜索了这个问题并进行了数小时的研究,但是这些解决方案似乎都不适合我,并且已经过时了。 I've read Apple's documentation, but it went over my head. 我已经阅读了Apple的文档,但是这让我头疼。 I'm still a beginner when it comes to programming. 在编程方面,我还是一个初学者。 Does anyone know of a simple way to set my custom image as the background for the UIView? 有谁知道将我的自定义图像设置为UIView背景的简单方法?

Thank you. 谢谢。

使用与您的视图框架大小相同的UIImageView

您也可以在代码中使用此语句。

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"foo.bar"]]];

Just drag-and-drop the image from Finder to the extreme left pane of your Xcode window (where the list of files is shown). 只需将图像从Finder拖放到Xcode窗口的最左侧窗格(显示文件列表)。 Now open your xib file and locate the image in your media library. 现在打开您的xib文件,并在媒体库中找到该映像。 Just drag-and-drop the image from the media library to your view. 只需将图像从媒体库拖放到视图中即可。 Adjust the size of the image as you need. 根据需要调整图像的尺寸。

If you want to change the background of all screens of the app then you can do it your AppDelegate.m file.First of all make all xib files background color clear color.Then use following code to change background color. 如果要更改应用程序所有屏幕的背景,则可以在AppDelegate.m文件中进行操作。首先使所有xib文件的背景色变为透明色,然后使用以下代码更改背景色。

self.window.backgroundColor = [UIColor redcolor];

If you want to make background color like an Image then use following code. 如果要使背景颜色像图像一样,请使用以下代码。

self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_image.1.png"]];

Swift 3.0 : Swift 3.0:

Above code for swift 3.0 is 上面的swift 3.0代码是

import UIKit
@IBOutlet weak var mainUIView: UIView!


class fileName: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    mainUIView.backgroundColor = UIColor(patternImage: UIImage(named: "your_image_name.png")!)

   }
}

Hope it Helps Some1 =] 希望它对Some1有帮助[]

if you're using Autolayout then you have to struggle a lot coz the image which you're going to add to the background, will not set itself perfectly , as we're doing Programmatically ! 如果您使用的是“自动布局”,那么您将不得不付出很多努力,因为要添加到背景中的图像不会像我们以编程方式那样完美地设置自身!

If anyone finds an answer of this question, and can be done by interface builder.. Than It would be helpful for all of us =D.. 如果有人找到了这个问题的答案,可以由界面生成器完成。.比这对我们所有人都很有帮助= D ..

Lastly Add a ImageView to that view ,that'll do the job. 最后,将ImageView添加到该视图,即可完成工作。

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"foo.bar"]]];

You can use this statement. 您可以使用此语句。 But you should notice that the size of the image should be the size of the view else it will be repeated. 但是您应该注意,图像的大小应该是视图的大小,否则它将被重复。 For the repeated image it works great. 对于重复的图像,效果很好。

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

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