我已经使用适用于 ios 11 的新 Xcode 9 beta 构建了我的应用程序。我发现 UITabBar 存在问题,其中项目通过 UITabBar 传播并且标题与图像右对齐。我已尝试更改代码以使其正常工作,但仍未成功。
iOS 10+
iOS 11
我可以使用 tabBarItem.titlePositionAdjustment
更改标题的位置
但这不是我的要求,因为它应该自动出现在图像本身的下方。我尝试将 tabbar.itemPositioning 设置为 UITabBarItemPositioningCentered
并尝试更改 itemSpacing
和 width
,但仍然无效。有人可以帮我理解为什么会发生这种情况以及如何解决这个问题吗?我希望它喜欢 ios 10+ 版本,并且图像是从 iPad 的最左角拍摄的。
最佳答案
我正在维护一个主要用 Objective-C 编写的大型 iPad 应用程序,该应用程序已在多个 iOS 版本中幸存下来。我遇到了这样一种情况,即我需要 iOS 11 之前的标签栏外观(图标在标题上方而不是在它们旁边)用于几个标签栏。我的解决方案是创建一个覆盖 traitCollection
方法的 UITabBar 子类,以便它始终返回一个水平紧凑的特征集合。这会导致 iOS 11 在所有标签栏按钮的图标下方显示标题。
为了使用它,将 Storyboard中标签栏的自定义类设置为这个新的子类,并将代码中指向标签栏的所有导出更改为这种新类型(不要忘记导入下面的头文件)。
在这种情况下,.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
// (i.e. 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
关于ios 11 UITabBar UITabBarItem 定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44822558/