How do you add a background to your main view?

Discussion in 'iOS Development' started by bamhm182, May 24, 2009.

  1. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Well, for my apps, when I wanted a background, I assumed that the correct way to do it was to add a UIImageView, but than Xcode yells at me for overlapping objects, so I'm assuming that's not the way you're supposed to do it. I've searched and come up empty handed. So what is the "right way" to add a background?

    Is it something like this, or am I WAY off?:

    Code:
    - (void)viewDidLoad
    {
           self.view.* = [UIImage imageNamed:@"MetalBackground.png"];
    }
  2. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Add a UIImageView to whatever view you want that is the same size as the view, and then add all your other views on top of that.
  3. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    That's what I've been doing, however, like I said, Xcode tells me "This view overlaps one of its siblings." So I assumed I was doing it wrong. You're sure this is the correct way to do it though? If so, fair enough, I suppose it would come in the form of a warning or an error if it was a big issue.
  4. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    It will say that it is overlapping, because it is overlapping your main view. Just put the UIImageView over the main view and it will be fine just ignore the warning.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [view addSubview:anotherView];
  6. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Either create an image view and set it:

    Code:
    UIImageView *myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Blah.png"]];
    [someView addSubview:myImage];
    [myImage release];
    (You may want to set the contentMode of the UIView you're adding this too also, as your image may not be 320x460, it's up to you.)


    Or set the view's background color as a UIColor with your image as a pattern:

    Code:
    someView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Blah.png"]];
    That may not be exact, and you also want to keep in mind that the above code is only setting the background color, so it will be covered up by any subviews added. But then again, so would an image view placed on there. The difference is adding an image view, the image view gets placed on the very top of the view, if you already had multiple subviews added, and then set the background color, you would not notice a change unless you didn't have other subviews covering up the background.
  7. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Thanks guys. I was originally trying to do it gojohhnyboi's second way, but wasn't sure how to set the color as an image. I'll mess around with the methods you gave me.

Share This Page