我以编程方式将标签栏项目添加到标签栏,但是当标签栏处于横向模式时,标签栏项目不会更改为左侧图标和右侧文本的新横向样式。当标签栏处于 iOS 11 的横向模式时,如何更新标签栏项目的样式?
这是一些应要求的代码。
UITabBar *tabBar = [UITabBar new];
NSInteger tag = 0;
NSMutableArray<UITabBarItem *> *items = [NSMutableArray new];
for(UIViewController *viewController in viewControllers)
{
NSString *title = viewController.title;
UIImage *image = [UIImage imageNamed:title];
UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:title image:image tag:tag++];
[items addObject:tab];
}
tabBar.items = items;
[self.view addSubview:tabBar];
最佳答案
当将方向更改为横向时,它应该会自动工作,我尝试过并且它在 Xcode 9.0 中工作,当您使用基于 Storyboard的 TabBar Controller 或您的自定义 TabBar Controller 时,不需要完成额外的代码。
在谷歌搜索时,我发现有人在其中一个图书馆 Here 中报告了同样的问题。
他们通过在方向改变时设置标签和图像的框架来解决这个问题。所以,如果有帮助,请看这里。
https://github.com/eggswift/ESTabBarController
关于iOS 11 中横向 UITabBar 中的 UITabBarItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46386320/