Search code examples
iphoneiosbackgrounduinavigationbaruitabbar

iOS Background Image Being Squished by Navbar


I have an app that is using a UITabBar and UINavigationBar. I am creating a UIImageView that contains my background image and then add that UIImageView to my self.view as a subview. This causes the background to appear "squished" vertically. The background image is not starting until the bottom of the NavBar and then runs down behind the TabBar.

Here's the code I'm using to add the background:

UIImageView* backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    [backgroundView setFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:backgroundView];

Any ideas about how to keep the actual size of the background would be greatly appreciated.


Solution

  • First off, you won't have 480 points of height to use with a UINavigationBar present on anything less than an iPhone/iPod 5. You should use self.view.frame.size.height to get the actual size. Really, you should just set the frame to the view's bounds.

    [backgroundView setFrame:self.view.bounds];
    

    Secondly, you should set the contentMode property of the UIImageView.

    [backgroundView setContentMode:UIViewContentModeScaleAspectFill]; //Set UIViewContentModeScaleAspectFit if you want to show the full image with the potential of a letterbox
    

    That should solve your problems.