UITabbar在iPad上图标和标题不居中对齐

3
我正在开发一个应用程序,使用Tabbarcontroller(有两个选项卡)。 在iphone上一切正常,但在ipad上,两个tab都会出现在屏幕的中心。
我想把每个选项卡的标题和图像显示在选项卡的中心(考虑到选项卡宽度是屏幕的一半)。
在寻找这些信息时,我遇到了UIEdgeInsetsMake。但是,这里并没有帮助我解决问题。
我不明白在哪里放置这段代码。我试图先将其放置在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个回答

5
您应该将此代码放在您的UITabBarController的viewWillLayoutSubviews方法中。
self.tabBar.itemPositioning = UITabBarItemPositioningFill;

Swift 4, 5

self.tabBar.itemPositioning = .fill

太棒了..简单易懂...:) - Jain_Taresh
使用最新的 Swift 版本,逻辑如下。 self.tabBar.itemPositioning = .fill - Ganesh
.fill 对我没用,还有其他人吗? - stackich

2

参考此答案,我建议您按照以下方式操作:

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

2 除法后,通过setItemWidth将平均值分配给UITabBar

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

2
请注意,这将设置应用程序中所有选项卡栏的宽度 - 通常您只会有一个,但您可能仍然希望将设置限制为每个选项卡栏(tabBarController.tabBar.itemWidth = width)。 - SVD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接