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

时间:2022-08-26 21:34:42

I am develop in objective-C. I want to change the tab bar background like the following picture: 如何在Objective-C中更改标签栏的背景图像?

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

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:

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

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

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.

提前致谢。

4 个解决方案

#1


2  

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

我认为是你出错的地方,检查这是否是这个步骤:

  1. In the storyboard change the ViewController's background color for test.
  2. 在故事板中更改ViewController的背景颜色以进行测试。

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

  1. Embed the ViewController in Tab Bar Controller
  2. 在窗口栏控制器中嵌入ViewController

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

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

    在ViewController.m中,您可以设置tabbar bacground颜色:

    - (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:

    结果如下:

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

#2


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.

我认为方法 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode你可以尝试,因为你的backgroudimage的大小不等于tabbar'size。

#3


0  

Try this method to change your image to a ScaleImage.

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

#4


0  

Steps you may miss, hope that can help.

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

  1. Make sure you have imported the background image (e.g. in Assets.xcassets)
  2. 确保已导入背景图像(例如,在Assets.xcassets中)
  3. Use resizableImageWithCapInsets: to resize the background image
  4. 使用resizableImageWithCapInsets:调整背景图像的大小
  5. Put the UIAppearance settings in AppDelegate.m:

    将UIAppearance设置放在AppDelegate.m中:

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

#1


2  

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

我认为是你出错的地方,检查这是否是这个步骤:

  1. In the storyboard change the ViewController's background color for test.
  2. 在故事板中更改ViewController的背景颜色以进行测试。

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

  1. Embed the ViewController in Tab Bar Controller
  2. 在窗口栏控制器中嵌入ViewController

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

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

    在ViewController.m中,您可以设置tabbar bacground颜色:

    - (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:

    结果如下:

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

#2


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.

我认为方法 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode你可以尝试,因为你的backgroudimage的大小不等于tabbar'size。

#3


0  

Try this method to change your image to a ScaleImage.

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

#4


0  

Steps you may miss, hope that can help.

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

  1. Make sure you have imported the background image (e.g. in Assets.xcassets)
  2. 确保已导入背景图像(例如,在Assets.xcassets中)
  3. Use resizableImageWithCapInsets: to resize the background image
  4. 使用resizableImageWithCapInsets:调整背景图像的大小
  5. Put the UIAppearance settings in AppDelegate.m:

    将UIAppearance设置放在AppDelegate.m中:

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