Only image as UITabBarItem

2019-03-10 02:39发布

I would only like to have an icon as the UITabBarItem and not the text underneath and I was wondering if this was possible, if so, how? TIA

9条回答
该账号已被封号
2楼-- · 2019-03-10 03:05

REMOVE THE TITLE

The easy way with inspector

  • Select your TabbarItem
  • In Title Position change to Custom Offset
  • Set vertical to 100
  • Done

enter image description here

Fix IMAGE

Adjuste the image

  • Select your TabbarItem
  • define your Top and Bottom

enter image description here

查看更多
仙女界的扛把子
3楼-- · 2019-03-10 03:07

I know its been quite some time. But I might have a solution for this problem.

@ThiagoPires answer is useful only if the image is big enough to cover the title. In case if it isn't, then the title will be visible under the image.

I think that the easiest way to accomplish this, is by setting the title of the tabBarItem to "" in your view controllers code.

It is important to know that every time you change your view controllers title, the tabbaritem's title will be updated too. You could go fancy using KVO to observe the title's change and set "" to the tabbaritem title accordingly. But if you don't change it in many places in your code, you could just set it right in the place you change the ViewController's title.

Here is an example in swift:

override func viewDidLoad() {

    super.viewDidLoad()

    self.title = "View controller's Title"

    self.tabBarItem.title = ""
}

That's it. I hope that this could help.

查看更多
我只想做你的唯一
4楼-- · 2019-03-10 03:11

Try with below code:

UIViewController *viewController1=[[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
UIViewController *viewController2=[[SecondViewCtr alloc]initWithNibName:@"SecondViewCtr" bundle:nil];
UIViewController *viewController3=[[ThirdViewCtr alloc]initWithNibName:@"ThirdViewCtr" bundle:nil];
UIViewController *viewController4=[[FourthVIewCtr alloc]initWithNibName:@"FourthVIewCtr" bundle:nil];
UIViewController *viewController5=[[FIfthViewCtr alloc]initWithNibName:@"FIfthViewCtr" bundle:nil];


UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:viewController5];
peekArray = [[NSMutableArray alloc] init];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2,nav3,nav4,nav5, nil];

UITabBar *tabBar = _tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];

[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home123.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home_112.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"gift123.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"gift_112.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"cam12.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cam_112.png"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"message12.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"message_112.png"]];
[tabBarItem5 setFinishedSelectedImage:[UIImage imageNamed:@"people12.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"people_112.png"]];

[self.tabBarController setSelectedIndex:0];
self.window.rootViewController = self.tabBarController;
查看更多
登录 后发表回答