Ios – custom tabbar background image is too big

iosiphoneobjective cuitabbar

I am trying to set a custom background image on my tabbar. I have image named "tabbarBack.png", with size of 640×92. In my code I am setting it like this.

[[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:@"tabbarBack.png"]];

When I test it on device the tabbar is twice bigger than it should be?
Any help?

Kind regards

Best Answer

Resizing your image may cause it to lose its resolution since it's pixel based. Instead of using setBackgroundImage (which will not allow you to resize the image) and altering your image outside of Xcode, why not insert the background image as a subview of the tab bar? This way you can resize the frame of the image in XCode and leave the image file untouched!

/* TAB BACKGROUND IMAGE */
UIImageView *tabBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 49)];
tabBackground.image = [UIImage imageNamed:@"BackgroundImage.png"];
tabBackground.contentMode = UIViewContentModeScaleAspectFill;
[self.tabBar insertSubview:tabBackground atIndex:0];

The default tab dimensions are 320x49 - adjust initWithFrame:CGRectMake above if your tab bar dimensions are custom. Lastly, if you have OTHER images that you are adding as subviews to the tab bar, make sure to add those before adding the background image.