set background image for entire iPhone / iPad app

IphoneObjective CIpad

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

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.

Solution 3 - Iphone

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

Solution 4 - Iphone

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

Solution 5 - Iphone

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

Solution 6 - Iphone

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.

Solution 7 - Iphone

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)
    }

Solution 8 - Iphone

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];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSleeView Question on Stackoverflow
Solution 1 - Iphonesudo rm -rfView Answer on Stackoverflow
Solution 2 - IphoneFelixyzView Answer on Stackoverflow
Solution 3 - IphoneManniView Answer on Stackoverflow
Solution 4 - IphoneanasaitaliView Answer on Stackoverflow
Solution 5 - IphoneAndrew ThekenView Answer on Stackoverflow
Solution 6 - IphoneBrooks HanesView Answer on Stackoverflow
Solution 7 - IphoneMina FawzyView Answer on Stackoverflow
Solution 8 - IphoneRicardoView Answer on Stackoverflow