如何删除UITabBarItem下面的空格?

时间:2021-12-03 06:09:39

I have created a tabBar and set image in it, but it leaves too much space below the tabBarItem. How can I remove that?

我已经创建了一个tabBar并在其中设置了图像,但它在tabBarItem下面留下了太多空间。我怎么能删除它?

This is my tabBar than displaying right now

这是我的tabBar而不是现在显示

如何删除UITabBarItem下面的空格?

And I want to display it like this

我希望像这样展示它

如何删除UITabBarItem下面的空格?

To display Tabbar

显示Tabbar

firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
thirdVC = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
forthVC = [[ForthViewController alloc] initWithNibName:@"ForthViewController" bundle:nil];

    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];

    NSArray *viewControllersArray = [[NSArray alloc] initWithObjects:firstVC,secondVC,thirdVC,forthVC, nil];

    self.tabController = [[UITabBarController alloc] init];
    [self.tabController setViewControllers:viewControllersArray animated:NO];


    [self.window addSubview:self.tabController.view];

    //self.tabController.selectedIndex = 1;

    self.tabController.delegate = self;

    self.window.rootViewController = self.tabController;
    [self.window makeKeyAndVisible];

For tabBar background image i have used this code

对于tabBar背景图片我使用了这段代码

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

And for set the imge at item i have used this code

并设置img项目我已使用此代码

//used to set the tabBarItem images
        [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"home_tab"] withFinishedUnselectedImage:[UIImage imageNamed:@"home_tab"]];
        //Set the badge on tabBarItem
        [self.tabBarItem setBadgeValue:@"15"];

2 个解决方案

#1


17  

As Vytis says in their answer here:

正如Vytis在答案中所说:

There is a property on UIBarItem (UIBarButton item inherits from this class) imageInsets.

UIBarItem上有一个属性(UIBarButton项继承自此类)imageInsets。

To use full height images (49px) for finishedSelectedImage and finishedUnselectedImage you need to set these image insets:

要为finishedSelectedImage和finishedUnselectedImage使用全高图像(49px),您需要设置这些图像插入:

tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

tabBarItem.imageInsets = UIEdgeInsetsMake(6,0,-6,0);

you have to write the following lines of code

你必须编写以下代码行

item0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

#2


9  

I think you should use image insect in the xib for this like ,如何删除UITabBarItem下面的空格?

我认为你应该在xib中使用image insect,就像

for your requirement top should be some positive value and bottom should be some same negative value . If you have added the tab bar programmatically then you can refer the Wills answer

对于你的要求,top应该是一些正值,而bottom应该是一些相同的负值。如果您以编程方式添加了选项卡栏,则可以参考Wills答案

#1


17  

As Vytis says in their answer here:

正如Vytis在答案中所说:

There is a property on UIBarItem (UIBarButton item inherits from this class) imageInsets.

UIBarItem上有一个属性(UIBarButton项继承自此类)imageInsets。

To use full height images (49px) for finishedSelectedImage and finishedUnselectedImage you need to set these image insets:

要为finishedSelectedImage和finishedUnselectedImage使用全高图像(49px),您需要设置这些图像插入:

tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

tabBarItem.imageInsets = UIEdgeInsetsMake(6,0,-6,0);

you have to write the following lines of code

你必须编写以下代码行

item0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

#2


9  

I think you should use image insect in the xib for this like ,如何删除UITabBarItem下面的空格?

我认为你应该在xib中使用image insect,就像

for your requirement top should be some positive value and bottom should be some same negative value . If you have added the tab bar programmatically then you can refer the Wills answer

对于你的要求,top应该是一些正值,而bottom应该是一些相同的负值。如果您以编程方式添加了选项卡栏,则可以参考Wills答案