我正在維護一個大型的iPad應用程序,主要編寫Objective-C,它已經存活了幾個iOS版本。我遇到了這種情況,我需要iOS 11以前的標籤欄外觀(圖標位於標題的上方,而不是旁邊),用於標籤欄。我的解決方案是創建一個UITabBar的子類,它覆蓋traitCollection
方法,以便它總是返回一個橫向緊湊的特徵集合。這會導致iOS 11在所有標籤欄按鈕的圖標下方顯示標題。
爲了使用它,將故事板中的標籤欄的自定義類設置爲這個新的子類,並將代碼中指向標籤欄的任何插座改爲這種新類型(不要忘記導入下面的頭文件)。
.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
這是一個功能,而不是一個錯誤。 – matt
這不僅僅是Objective-C的問題,而且通常與Objective-C和Swift應用程序都有關係。 – Jeet
功能? IDK的。更多的情況是蘋果「改善」了一些東西,卻沒有考慮到所有的後果。 –