1

I am develop in objective-C. I want to change the tab bar background like the following picture: enter image description here

And the code is like the following:

UIImage *tabBarBackground = [UIImage imageNamed:@"tabbaritem_background.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];

But after setting the background image , the background is not at the correct place like the following:

enter image description here

The background image should be place at the bottom like the background in above picture.

Did I missing something ? Can someone help me ?

Thanks in advance.

Community
  • 1
  • 1
Wun
  • 6,211
  • 11
  • 56
  • 101
  • Do these help? http://stackoverflow.com/questions/8909379/setting-a-background-image-for-a-tabbar and http://stackoverflow.com/questions/13413651/how-to-set-a-custom-background-image-to-tabbar . If not, you may need to explain the problem more clearly. – Son of a Beach Feb 17 '17 at 01:16
  • Show we your tabbaritem_background.png – Tinyfool Feb 17 '17 at 01:18
  • @SonofaBeach , I have try the code in the link , but it is the same...the background image should be place at the bottom like the background in the top of picture. – Wun Feb 17 '17 at 01:25
  • Are you saying that it is placed somewhere else, or that it is not appearing anywhere at all? (The latter, I think?) Please log the following, to make sure they are as expected: tabBarBackground (UIImage) & the tabBar itself. – Son of a Beach Feb 17 '17 at 01:27
  • Do you have an image to match the resolution of your device (eg, 2x for a retina, or 3x for a recent iPad)? – Son of a Beach Feb 17 '17 at 01:32

4 Answers4

1

I think is somewhere you go wrong, check if is this steps:

  1. In the storyboard change the ViewController's background color for test.

enter image description here

  1. Embed the ViewController in Tab Bar Controller

enter image description here

  1. In the ViewController.m you can set the tabbar bacground color:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        [[UITabBar appearance] setBackgroundColor:[UIColor grayColor]]; // Here you can set the converted color form image, make sure the imageSize fit.
    }
    
  2. The result is below:

aircraft
  • 25,146
  • 28
  • 91
  • 166
0

I think the method - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode you can try, because your backgroudimage's size is not equal to tabbar'size.

Mistrx丶
  • 267
  • 3
  • 10
0

Try this method to change your image to a ScaleImage.

+(UIImage *)getScaleImageNamed:(NSString *)name{
    UIImage *nomalImage = [UIImage imageNamed:name];

    CGFloat hInset = floorf(nomalImage.size.width / 2);
    CGFloat vInset = floorf(nomalImage.size.height / 2);

    UIImage *res = [nomalImage resizableImageWithCapInsets:UIEdgeInsetsMake(vInset, hInset, vInset, hInset)];

    return res;
}
Danny Lau
  • 137
  • 8
0

Steps you may miss, hope that can help.

  1. Make sure you have imported the background image (e.g. in Assets.xcassets)
  2. Use resizableImageWithCapInsets: to resize the background image
  3. Put the UIAppearance settings in AppDelegate.m:

    [[UITabBar appearance] setBackgroundImage:[[UIImage imageNamed:@"tabbaritem_background.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];
    
Chris So
  • 711
  • 1
  • 11
  • 23