Adding A Background Image In Interface Builder

2020-07-03 04:44发布

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. I can add plain colors as the background in Interface Builder, but I do not see the option to add a custom image. 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. 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?

Thank you.

6条回答
你好瞎i
2楼-- · 2020-07-03 04:48

Swift 3.0 :

Above code for swift 3.0 is

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 =]

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..

Lastly Add a ImageView to that view ,that'll do the job.

查看更多
Viruses.
3楼-- · 2020-07-03 04:54

Use a UIImageView that is the size of your views frame.

查看更多
萌系小妹纸
4楼-- · 2020-07-03 05:02

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.

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"]];
查看更多
Juvenile、少年°
5楼-- · 2020-07-03 05:06
[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.

查看更多
▲ chillily
6楼-- · 2020-07-03 05:07

Just drag-and-drop the image from Finder to the extreme left pane of your Xcode window (where the list of files is shown). Now open your xib file and locate the image in your media library. Just drag-and-drop the image from the media library to your view. Adjust the size of the image as you need.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-07-03 05:08

You could also use this Statement in Code.

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"foo.bar"]]];
查看更多
登录 后发表回答