如何在Objective-C中更改标签栏的背景图像?

时间:2017-02-17 01:12:31

标签: ios objective-c tabbar

我在Objective-C中发展。我想更改标签栏背景,如下图所示: enter image description here

代码如下:

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

但是在设置背景图片后,背景不在正确的位置,如下所示:

enter image description here

背景图片应放在底部,就像上图中的背景一样。

我错过了什么吗?有人能帮助我吗?

提前致谢。

4 个答案:

答案 0 :(得分:2)

我认为在某个地方你出错了,请检查这是否是这个步骤:

  1. 在故事板中更改ViewController的背景颜色以进行测试。
  2. enter image description here

    1. 将ViewController嵌入标签栏控制器
    2. enter image description here

      1. /* this is to test whether the program ignores imbalances in the comment blocks */ public class Test3 { public static void main(String[] args) { boolean haveYouWatchedHamiltonYet = true; int theSchuylerSisters = 3; int alexanderhamilton = 1; int aaronburr = 1; boolean amIintheroom = theRoomWhereItHappens(); /* this is a commented block. We're testing if your code can deal with unbalanced things in the comments: /* that was the test for here */ } /*just a general comment */ /* this one has some errors /* { [ { [ } ] */ public boolean theRoomWhereItHappens() { boolean intheRoomWhereItHappens = false; boolean isHappyAboutThis = false; return intheRoomWhereItHappens && isHappyAboutThis; } } 中,您可以设置标签栏免费颜色:

        ViewController.m
      2. 结果如下:

答案 1 :(得分:0)

我认为您可以尝试使用方法- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode,因为您的背景图像的大小不等于tabbar&size;

答案 2 :(得分:0)

尝试使用此方法将图像更改为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;
}

答案 3 :(得分:0)

您可能会错过的步骤,希望可以提供帮助。

  1. 确保您已导入背景图片(例如,在Assets.xcassets
  2. 使用resizableImageWithCapInsets:调整背景图片大小
  3. UIAppearance设置放在AppDelegate.m

    [[UITabBar appearance] setBackgroundImage:[[UIImage imageNamed:@"tabbaritem_background.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];