ios 11 UITabBar UITabBarItem定位问题

我已经build立了我的应用程序使用新的Xcode 9testing版的ios 11.我已经发现UITabBar项目通过UITabBar传播和问题是右alignment的图像的问题。 我试图改变代码来使它工作,但仍然不成功。

ios 10+

在这里输入图像说明

ios 11

在这里输入图像说明

我可以改变标题的位置使用tabBarItem.titlePositionAdjustment但是,这不是我的要求,因为它应该自动来图像本身下面。 我试着设置tabbar.itemPositioning to UITabBarItemPositioningCentered ,也试着改变itemSpacingwidth ,但仍然没有工作。 有人能帮我理解为什么发生这种情况,以及如何解决这个问题? 我想要它喜欢ios 10+版本和图像取自iPad的最左angular。

我正在维护一个大型的iPad应用程序,这个应用程序大部分都是在Objective-C中编写的,这个应用程序在多个iOS版本中都已经存在 我遇到了这种情况,我需要iOS 11以前的标签栏外观(在标题上方,而不是旁边的图标)。 我的解决scheme是创build一个UITabBar的子类,覆盖traitCollection方法,以便它总是返回一个水平紧凑的特征集合。 这会导致iOS 11在所有标签栏button的图标下方显示标题。

为了使用这个,将故事板中的标签栏的自定义类设置为这个新的子类,并将代码中指向标签栏的任何插口更改为这种新types(不要忘记导入头文件下面)。

在这种情况下.h文件几乎是空的:

 // // MyTabBar.h // #import <UIKit/UIKit.h> @interface MyTabBar : UITabBar @end 

下面是带有traitCollection方法实现的.m文件:

 // // MyTabBar.m // #import "MyTabBar.h" @implementation MyTabBar // In iOS 11, UITabBarItem's have the title to the right of the icon in horizontally regular environments // (ie the iPad). In order to keep the title below the icon, it was necessary to subclass UITabBar and override // traitCollection to make it horizontally compact. - (UITraitCollection *)traitCollection { return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact]; } @end 

为了避免混淆其他特性,与超类结合是不是更好:

 - (UITraitCollection *)traitCollection { UITraitCollection *curr = [super traitCollection]; UITraitCollection *compact = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact]; return [UITraitCollection traitCollectionWithTraitsFromCollections:@[curr, compact]]; } 

基于John C的回答,下面是可以以编程方式使用而不需要Storyboard或子类的Swift 3版本:

 extension UITabBar { // Workaround for iOS 11's new UITabBar behavior where on iPad, the UITabBar inside // the Master view controller shows the UITabBarItem icon next to the text override open var traitCollection: UITraitCollection { if UIDevice.current.userInterfaceIdiom == .pad { return UITraitCollection(horizontalSizeClass: .compact) } return super.traitCollection } } 

注意: 上面的修复似乎很好地工作。 不知道将来如何工作,但现在它工作得很好。


根据这个WWDC的谈话 ,这是新的行为:

  • 横向的iPhone
  • iPad一直都在

如果我正确阅读,这个行为是不能改变的:

我们为标签栏提供了全新的外观,无论是在横向还是在iPad上,我们都将图标和文本并排显示。 特别是在iPhone上,图标更小,标签栏更小,垂直空间更多。

除了约翰的答案:

如果你有超过4个标签栏项目,不想要“更多”button,你必须采取不同的大小类。 如果你想要原始的中心布局的项目,你必须添加另一种方法,如:

 #import "PreIOS11TabBarController.h" @interface PreIOS11TabBarController () @end @implementation PreIOS11TabBarController // In iOS 11, UITabBarItem's have the title to the right of the icon in horizontally regular environments // (ie the iPad). In order to keep the title below the icon, it was necessary to subclass UITabBar and override // traitCollection to make it horizontally compact. - (UITraitCollection *)traitCollection { return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassUnspecified]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; self.tabBar.itemPositioning = UITabBarItemPositioningCentered; } @end