ios 11 UITabBar UITabBarItem 定位问题

标签 ios objective-c uitabbar uitabbaritem ios11

我已经使用适用于 ios 11 的新 Xcode 9 beta 构建了我的应用程序。我发现 UITabBar 存在问题,其中项目通过 UITabBar 传播并且标题与图像右对齐。我已尝试更改代码以使其正常工作,但仍未成功。

iOS 10+

enter image description here

iOS 11

enter image description here

我可以使用 tabBarItem.titlePositionAdjustment 更改标题的位置 但这不是我的要求,因为它应该自动出现在图像本身的下方。我尝试将 tabbar.itemPositioning 设置为 UITabBarItemPositioningCentered 并尝试更改 itemSpacingwidth,但仍然无效。有人可以帮我理解为什么会发生这种情况以及如何解决这个问题吗?我希望它喜欢 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/

相关文章:

objective-c - 我可以在弹出窗口消失时触发通知吗 (iPad)

objective-c - 简单的 NSImage 绘图无法按预期工作

ios - UITableView滚动不滚动

ios - 回收标签栏空间

ios - 具有自定义逻辑的可重用 UITableViewCell

ios - 用于 2 个 TableViews/Segmented Control Swift 的自定义单元格

ios - 如何在 Swift 中取消正在进行的 HTTP 请求?

ios - mpmovieplayercontroller : Playing in background

objective-c - 在 Objective-C 中取消引用指针

ios - 如何更改 tabBar 中未选择图片的色调