I have a single image I want as the background for my app no matter what viewcontroller they are on - how do you accomplish this?
8 Answers
Here's how you set a background to an image:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
Edit: To write up what Felixyz said (and thanks to Manni), do this in your delegate:
window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
And in each view you want to have the image, do this:
self.view.backgroundColor = [UIColor clearColor];

- 29,408
- 19
- 102
- 161
-
7I ended up using an UIImageView on the main window nib and UIColor clearColor on my views. Note that using the colorWithPatternImage as you state above made my memory use jump from 1.7M to 9.5M when the app is first loaded - colorWithPatternImage seems to just eat up a ton of memory. – Slee Feb 10 '11 at 13:38
-
1It's recommended to prepare 3 png files for your Universal app, says Background.png, Background@2x.png, Background~iPad.png, and set the background using below code: self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background"]]; iOS will select the correct png file due to your device category. Hope this help. – ZYiOS Mar 03 '12 at 03:38
-
I tried the above approach in a `UINavigation` based application .the problem with with this approach is that setting a image on the window.background set the image properly but making the other view clear color is bad as during push or pop view are overlapping you can see the views pushed in the stack .Any idea how to get it fixed – Shekhu Aug 07 '14 at 06:11
-
I solved the problem from Aiden by setting the background color, as suggested in this answer, to the navigation controller view background. Then, as I'm using Storyboards, I'm replacing the standard push animation (with sliding views) with a custom UIStoryboardSegue that implements a fading effect: the image remains stable and the effect lamented by Aiden is no longer visible. In my case I'm not using navigation bars too, they are all hidden (with the exception of modal views). – viggio24 Dec 22 '14 at 09:27
-
@viggio24 how to create "custom UIStoryboardSegue that implements a fading effect" exactly? – He Yifei 何一非 Aug 24 '15 at 01:56
-
@Arefly There are plenty of tutorials on how to do this. One of them is: http://blog.jimjh.com/a-short-tutorial-on-custom-storyboard-segues.html where you will replace the flipping animation with an animation on opacity (alpha). – viggio24 Aug 26 '15 at 13:23
Depends on what sort of interface you have. Tabbed? Navigation based? But the general answer is: add a UIImageView to your UIWindow before/below your main view. Then make every view handled by your main view controller have a transparent background. Hard to give more specific advice without knowing if you use IB or not, or what your view hierarchy looks like.

- 19,053
- 14
- 65
- 60
-
1Navigation based, so as view controllers are pushed and popped the background stays – Slee Feb 06 '11 at 21:46
-
Combining sudo's and Manni's suggestions makes it easy to accomplish what you need: set the backgroundColor on the window to be an image (colorWithPatternImage:) and make backgroundColor on the navigation controller and all its sub-controllers transparent. You might have to set opaque = NO on them too. Be aware that this might affect performance during animations, but shouldn't be a problem on newer devices. – Felixyz Feb 06 '11 at 23:07
In my app, I set a default background color. Maybe you can do this with you background image:
1.: Set the background color of your UIWindow in your AppDelegate:
window.backgroundColor = [UIColor myBackgroundGray]; // own Category
2.: And now, make all other views transparent:
self.view.backgroundColor = [UIColor clearColor]; // = transparent

- 11,108
- 15
- 49
- 67
-
1This causes a poor ux when presenting modal presenters with animation. You can see the controls from the modal view and the existing view and then the existing view disappears. – Καrτhικ Sep 10 '12 at 19:03
In your AppDelegate in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Add this line :
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
After you just have to set your views backgrounds to [UIColor clearColor];

- 1,504
- 21
- 30
I am not sure of the performance implications, but I needed to accomplish something similar, and ended up using a UIImageView which works well (in C#, but works the same in obj-c):
//Add the view controller first, to ensure proper order of views later.
Window.RootViewController = new UIViewController();
//create backdrop image view
var imageView = new UIImageView(Window.Bounds);
imageView.Image = UIImage.FromBundle("backdrop.jpg");
//insert into window.
Window.InsertSubview(imageView, 0);
This doesn't handle orientation changes, but in my case, allowed me to add motion effects to the backdrop (such as parallax).

- 3,392
- 1
- 31
- 54
just call this assignbackground
in viewDidLoad
override func viewDidLoad() {
assignbackground()
}
func assignbackground(){
let background = UIImage(named: "background")
var imageview : UIImageView!
imageview = UIImageView(frame: view.bounds)
imageview.contentMode = UIViewContentMode.ScaleAspectFill
imageview.clipsToBounds = true
imageview.image = background
imageview.center = view.center
view.addSubview(imageview)
self.view.sendSubviewToBack(imageview)
}

- 20,852
- 17
- 133
- 156
Your background is a property of any View-inheriting object. Labels, Buttons, Controllers, and the app window, for example, all have backgrounds. If you want it to be completely a bg for the entire app you must climb the path in your controllers to find the very "top" (bottom-viewed) view, and set its background to be the image you desire.

- 425
- 2
- 12
I usually use this function to avoid overlaps with navigation bar on iphone.
-(void)setImageBackground:(NSString*)imageName{
UINavigationController* navigationController = [self navigationController];
float height = navigationController.toolbar.frame.size.height;
CGSize size = self.view.frame.size;
size.height = size.height;
UIGraphicsBeginImageContext(size);
CGRect bounds = self.view.bounds;
bounds.origin.y = bounds.origin.y + height;
bounds.size.height = bounds.size.height-height;
[[UIImage imageNamed:imageName] drawInRect:bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}

- 7,921
- 14
- 64
- 111