How to set background image of a view?

103,641

Solution 1

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]];

more info with example project

Solution 2

Besides all of the other responses here, I really don't think that using backgroundColor in this way is the proper way to do things. Personally, I would create a UIImageView and insert it into your view hierarchy. You can either insert it into your top view and push it all the way to the back with sendSubviewToBack: or you can make the UIImageView the parent view.

I wouldn't worry about things like how efficient each implementation is at this point because unless you actually see an issue, it really doesn't matter. Your first priority for now should be writing code that you can understand and can easily be changed. Creating a UIColor to use as your background image isn't the clearest method of doing this.

Solution 3

use this

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default"]];

Solution 4

simple way :

   -(void) viewDidLoad {
   self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage       imageNamed:@"background.png"]];
    [super viewDidLoad];
    }

Solution 5

It's a very bad idea to directly display any text on an irregular and ever changing background. No matter what you do, some of the time the text will be hard to read.

The best design would be to have the labels on a constant background with the images changing behind that.

You can set the labels background color from clear to white and set the from alpha to 50.0 you get a nice translucent effect. The only problem is that the label's background is a stark rectangle.

To get a label with a background with rounded corners you can use a button with user interaction disabled but the user might mistake that for a button.

The best method would be to create image of the label background you want and then put that in an imageview and put the label with the default transparent background onto of that.

Plain UIViews do not have an image background. Instead, you should make a UIImageView your main view and then rotate the images though its image property. If you set the UIImageView's mode to "Scale to fit" it will scale any image to fit the bounds of the view.

Share:
103,641
Daniel
Author by

Daniel

Updated on November 29, 2020

Comments

  • Daniel
    Daniel over 3 years

    I am a beginner at Obj-C/Cocoa Touch/iPhone OS.

    I wish to have a background for my app with different images everytime the the view is called.

    Say I have 10 images. I 've used it like this:

    //random image generation
    NSString* imageName;
    int aRandomNumber = arc4random() % 10;
    imageName =[NSString stringWithFormat:@"g%d.jpg",aRandomNumber]; 
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];
    NSLog(@"aRandomNumber is %d", aRandomNumber);
    //random image is generated
    

    Its working fine

    • Now, say I have text labels on my view and the text isn't displaying correctly due to image colors. How can I make it a little transparent? (I guess in Interface Builder its called alpha.)
    • Say my image isn't 320x480. How do I set it to fill the entire view?

    How can I do it with UIView/UIImageView?

    I found initWithHue:saturation:brightness:alpha: in the documentation but it's not working:

    self.view.backgroundColor = [[UIColor alloc] initWithHue:0.0 saturation:1.0 brightness:1.0 alpha:1.0];
    

    Please Help!


    A friend suggested........

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];
    

    ..........he told it's more efficient because it doesn't save the image in the cache.