Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom tabbar background image is too big

I am trying to set a custom background image on my tabbar. I have image named "tabbarBack.png", with size of 640x92. 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

like image 444
Steaphann Avatar asked Dec 17 '12 10:12

Steaphann


2 Answers

try this bellow two lines

self.tabBarController.tabBar.autoresizesSubviews = NO;
self.tabBarController.tabBar.clipsToBounds = YES;
like image 41
Paras Joshi Avatar answered Oct 02 '22 22:10

Paras Joshi


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.

like image 194
etayluz Avatar answered Oct 02 '22 21:10

etayluz