UITabbar图像和标题在ipad中不对齐

时间:2014-05-24 15:02:12

标签: ios ipad uitabbarcontroller uiedgeinsets

我正在使用Tabbarcontroller的应用程序(有两个标签) 在iphone上一切正常,但在ipad上,两个标签都出现在屏幕中央

我想在每个标签的中心显示标题和图像(考虑标签宽度是屏幕的一半)

在搜索这些内容时,我遇到了UIEdgeInsetsMake

Adjust iOS TabBar item title to the centre of it但没有帮助。

我不明白把这段代码放在哪里。我试着把它放在FirstViewController然后在app delegate中(逐个)但是没有什么对我有用

这是我的appDelegete代码:

FirstViewController *firstVC = [[FirstViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:firstVC];
//navController1.title = @"First Tab";
navController1.tabBarItem.image = [UIImage imageNamed:@"first.png"];

SecondViewController *secondVC = [[SecondViewController alloc] init];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondVC];
navController2.title = @"Second Tab";
navController2.tabBarItem.image = [UIImage imageNamed:@"second.png"];

NSArray *array = [[NSArray alloc] initWithObjects:navController1,navController2, nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
 tabBarController.tabBarItem.imageInsets = UIEdgeInsetsMake(0, -50, 0, 50);

tabBarController.viewControllers = array;

[self.window setRootViewController:tabBarController];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:5)

您应该将此代码放在UITabBarController的viewWillLayoutSubviews中

nativescript-intro-slides

答案 1 :(得分:2)

参考this回答我建议你这样做

1获取屏幕宽度并将其除以您拥有的标签栏项目数

2分割后,通过setItemWidth将{38}的平均值分配给UITabBar

CGRect screenSize = [[UIScreen mainScreen] applicationFrame];
int count  = [array count];
float width  = screenSize.size.width/count;
[[UITabBar appearance] setItemWidth:width];