简体   繁体   中英

Setting the height of UITabBar

I've created a simple custom tabbar by setting the images of each item, as following:

UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];

[item0 setFinishedSelectedImage:[UIImage imageNamed:@"activity_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"activity.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"agenda_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"agenda.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"settings_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]];

While this works great, I noticed that there is a black blank space under my tabbar

在此输入图像描述

My images are 44px in height, but I think I have to somehow change the height of my tabbar.

The tabBar itself is 49px, and it is rendered in black color behind your images (perhaps in [UITabBar layoutSubviews] ). Then your images are rendered on top. The reason of the offset is because the images you supply are too large, UITabBar expects 30x30px icons, not a picture of the entire UITabBarItem.

Here's a few things to try:

  1. Supply only a 30x30px icon, instead of the entire tab button
  2. After you set your images on the tab item, try this: [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)]; // play with insets until it renders correctly [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)]; // play with insets until it renders correctly
  3. Subclass UITabBar and override layoutSubviews to first call super , then reposition your image as you like. Not recommended, it might break in future iOS versions.

Use -

tabBar.frame=CGRectMake(x,y,w,h);

In this way you can set xCord, yCord, width and height.

检查一下:

[self.tabBar setFrame:CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y - 30, self.tabBar.frame.size.width, self.tabBar.frame.size.height + 30)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM