Reputation: 2777
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
Upvotes: 5
Views: 8517
Reputation: 50139
NSAddict's comment: Rename your image to [email protected]. This is called pixel doubling for the Retina Display.
Without the @2x iOS doesn't know that it should apply a scale factor and it will be used as it is and though it should be halved.
So a hack is:
[[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:@"[email protected]"]];
In reality there should be
so you say only
[[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:@"tabbarBack.png"]];
Upvotes: 1
Reputation: 694
Check if you have this line:
[[UITabBar appearance] setShadowImage:image];
You have to remove it or set to nil.
Upvotes: 1
Reputation: 16426
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.
Upvotes: 7
Reputation: 20551
try this bellow two lines
self.tabBarController.tabBar.autoresizesSubviews = NO;
self.tabBarController.tabBar.clipsToBounds = YES;
Upvotes: 5