Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Background Image into View Controller

I tried to use this code under in my app delegate in order to add a PNG image as a view controller's background :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[self window] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];

    return YES;
}

but I have no luck with it... the view controller still has a white background. What's wrong with that code?

like image 789
Saint Robson Avatar asked Sep 10 '13 13:09

Saint Robson


People also ask

How do you put a background picture on storyboard?

Go to 'View->Show Library' and choose the Image View and drag it to the middle of the screen until it snaps perfectly in the center. This Image View will serve as the background image and it will fill the entire screen, including the area past the safe area view.

How do I add an image to Xcode?

Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.

How do I change the background in Swift?

Find the view or view controller you want to change the background color of. Open it up in the interface builder and open the Attributes Inspector. Find the background color field and set it to the color you want.

How do I change the background in Xcode?

Select the view that you want to have a background image in Xcode. Create an image view in that view and resize it to fill the screen. Now open up the Attributes Inspector and select the correct Content Mode. There you go!


2 Answers

The accepted answer and Michael's answer will work, however, proper way is to use a UIImageView instead. It gives more control over resizing, scaling etc according to different screen sizes on devices. Here is the example;

First create a UIImage.

UIImage *backgroundImage = [UIImage imageNamed:@"iphone_skyline3.jpg"];

Second create a UIImageView. Set the frame size to the parent's (self) frame size. This is important as the frame size will vary on different devices. Stretching will occur depending on the image size. Next assign the image to the view.

UIImageView *backgroundImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
backgroundImageView.image=backgroundImage;

Finally, to keep the image behind all controls do the following. It is important if you are setting the image as a background for your app.

[self.view insertSubview:backgroundImageView atIndex:0];
like image 129
hadaytullah Avatar answered Sep 25 '22 13:09

hadaytullah


You need to set the background for your ViewController's view

In your ViewController init or viewDidLoad:

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
like image 29
Tarek Hallak Avatar answered Sep 21 '22 13:09

Tarek Hallak