UITabBarController系列——TabBarItem文案在iOS 13.3變成省略號(hào)

問題

兼容性測(cè)試發(fā)現(xiàn) iOS 13.3的iPhone 8 Plus上食店,一個(gè)TabBar頁面底部BarItem文案變成省略號(hào)


8p-rect.png

影響范圍

設(shè)備類型 結(jié)果
iOS >= 13.4的iPhone 8 Plus 正常
iOS 13.0~13.3的劉海屏iPhone 異常省略號(hào)
iOS 13.0~13.3的其他非劉海屏iPhone 異常省略號(hào)
iOS < 13.0的iPhone 8 Plus 正常

問題排查

iOS 13開始尊浪,UITabBar樣式設(shè)置用standardAppearance后专,并且iOS 15又新增了scrollEdgeAppearance匈睁。所以一般寫如下兼容性代碼:

- (void)setUpTabBarAppearance {
    UIViewController *codeVC = [[UIViewController alloc] init];
    codeVC.tabBarItem.title = @"掃碼1";
    [self addChildViewController:codeVC];

    UIViewController *ticketVC2 = [[UIViewController alloc] init];
    ticketVC2.tabBarItem.title = @"掃碼2";
    [self addChildViewController:ticketVC2];

……………………

    UIColor *selectedColor = kSelectedColor;
    NSDictionary *textAttNormal = @{ NSForegroundColorAttributeName : [UIColor whiteColor],
                                     NSFontAttributeName : [UIFont systemFontOfSize:18]};
    NSDictionary *textAttSelected = @{ NSForegroundColorAttributeName : selectedColor,
                                       NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
    
   if (@available(iOS 13.0, *)) {
        UITabBarAppearance * tabBarAppearance = [UITabBarAppearance new];

        UITabBarItemAppearance *barItemAppearance= [[UITabBarItemAppearance alloc] initWithStyle:UITabBarItemAppearanceStyleInline];
        barItemAppearance.normal.titleTextAttributes = textAttNormal;
        barItemAppearance.selected.titleTextAttributes = textAttSelected;

        tabBarAppearance.stackedLayoutAppearance = barItemAppearance;
        tabBarAppearance.stackedLayoutAppearance = barItemAppearance;
        
        self.tabBar.standardAppearance = tabBarAppearance;

        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = tabBarAppearance;
        }
    }
    else {
        // 問題出在這里
        [self.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj.tabBarItem setTitleTextAttributes:textAttNormal forState:UIControlStateNormal];
            [obj.tabBarItem setTitleTextAttributes:textAttSelected forState:UIControlStateSelected];
        }];
    }
}

查看UILabel尺寸

用Xcode自帶的Debug View Hierarchy查看界面元素,確定TabBarItem的文案是UILabel洽议,內(nèi)容低匙、樣式都是上面設(shè)置的。對(duì)比正常避消、異常的情況低滩,發(fā)現(xiàn)異常時(shí)UILabel的尺寸不同


8p-rect.png

打印UILabel

在viewDidLayoutSubviews中、viewDidAppear中打印UILabel

// 不使用UITabBarController.selectedIndex, if the selected view controller is currently the More navigation controller, this property contains the value NSNotFound.
    NSUInteger index = [self.tabBar nvm_selectedIndex];
    UIView *tabBarView = [self.tabBar nvm_tabBarButtonAtIndex:index];

    for (UILabel *obj in tabBarView.subviews) {
        if ([obj isKindOfClass:[UILabel class]]) {
            NSLog(@"== Label %@ : %@", obj.text, obj.font);
        }
    }

發(fā)現(xiàn)viewDidLayoutSubviews時(shí)岩喷,文字已生效恕沫,但字號(hào)未生效。

== Label 掃碼1 : <UICTFont: 0x139eaaca0> font-family: ".SFUI-Medium"; font-weight: normal; font-style: normal; font-size: 10.00pt

而viewDidAppear時(shí)纱意,文字婶溯、字號(hào)才設(shè)置上,但字號(hào)并未生效。此時(shí)調(diào)用sizeToFit可觸發(fā)生效迄委。同時(shí)注意到UICTFont屬性的實(shí)例對(duì)象變了褐筛。

== Label 掃碼1 : <UICTFont: 0x139f67740> font-family: ".SFUI-Semibold"; font-weight: bold; font-style: normal; font-size: 18.00pt

至此,明確問題原因在文字屬性未生效叙身。

返回去看上面代碼:title設(shè)置都會(huì)執(zhí)行渔扎,而字號(hào)設(shè)置,>= iOS 13信轿、 < iOS 13的設(shè)備會(huì)執(zhí)行不同邏輯晃痴,推測(cè)@available(iOS 13.0, *) 塊中的代碼導(dǎo)致titleTextAttributes未生效問題。

看起來是iOS 13.0~13.3上财忽,standardAppearance會(huì)延遲生效倘核?

嘗試1

@available(iOS 13.0, *)改為@available(iOS 13.4, *)
titleTextAttributes部分生效,省略號(hào)問題解決即彪,文字大小展示正確笤虫,但未選中狀態(tài)顏色不符合預(yù)期

嘗試2

看代碼的else代碼塊,在 < iOS 13.0時(shí)祖凫,用UIBarItem setTitleTextAttributes設(shè)置樣式,查看此方法:

/* You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in NSAttributedString.h.
 */
- (void)setTitleTextAttributes:(nullable NSDictionary<NSAttributedStringKey,id> *)attributes forState:(UIControlState)state API_AVAILABLE(ios(5.0)) UI_APPEARANCE_SELECTOR;

注意到setTitleTextAttributes并未廢棄酬凳,可以在任何版本上調(diào)用惠况。所以先統(tǒng)一設(shè)置UIBarItem的setTitleTextAttributes,再針對(duì)iOS 13.0以上設(shè)置standardAppearance宁仔,嘗試后問題解決稠屠。

解決方案

最終解決方案,先繼續(xù)設(shè)置未廢棄的舊方法翎苫,再根據(jù)iOS設(shè)置新方法权埠。此種方案,作為日后新接口適配的策略執(zhí)行下去煎谍。

附上正確代碼:

- (void)setUpTabBarAppearance {
    UIViewController *codeVC = [[UIViewController alloc] init];
    codeVC.tabBarItem.title = @"掃碼1";
    [self addChildViewController:codeVC];

    UIViewController *ticketVC2 = [[UIViewController alloc] init];
    ticketVC2.tabBarItem.title = @"掃碼2";
    [self addChildViewController:ticketVC2];

……………………

    UIColor *selectedColor = kSelectedColor;
    NSDictionary *textAttNormal = @{ NSForegroundColorAttributeName : [UIColor whiteColor],
                                     NSFontAttributeName : [UIFont systemFontOfSize:18]};
    NSDictionary *textAttSelected = @{ NSForegroundColorAttributeName : selectedColor,
                                       NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};

    // 在iOS 13.3上攘蔽,UITabBarAppearance在首次appear時(shí)不生效,包括viewDidLayoutSubviews結(jié)束時(shí)呐粘,而iOS 13.4開始無此問題
    // 因此無論iOS版本满俗,都設(shè)置老方法,解決兼容性問題
    [self.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.tabBarItem setTitleTextAttributes:textAttNormal forState:UIControlStateNormal];
        [obj.tabBarItem setTitleTextAttributes:textAttSelected forState:UIControlStateSelected];
    }];
    
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance * tabBarAppearance = [UITabBarAppearance new];

        UITabBarItemAppearance *barItemAppearance= [[UITabBarItemAppearance alloc] initWithStyle:UITabBarItemAppearanceStyleInline];
        barItemAppearance.normal.titleTextAttributes = textAttNormal;
        barItemAppearance.selected.titleTextAttributes = textAttSelected;
        tabBarAppearance.stackedLayoutAppearance = barItemAppearance;
        tabBarAppearance.stackedLayoutAppearance = barItemAppearance;
        
        self.tabBar.standardAppearance = tabBarAppearance;
        
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = tabBarAppearance;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末作岖,一起剝皮案震驚了整個(gè)濱河市唆垃,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌痘儡,老刑警劉巖辕万,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡渐尿,警方通過查閱死者的電腦和手機(jī)挺智,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铡俐,“玉大人或听,你說我怎么就攤上這事∮嬲茫” “怎么了嵌屎?”我有些...
    開封第一講書人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)恍涂。 經(jīng)常有香客問我宝惰,道長(zhǎng),這世上最難降的妖魔是什么再沧? 我笑而不...
    開封第一講書人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任尼夺,我火速辦了婚禮,結(jié)果婚禮上炒瘸,老公的妹妹穿的比我還像新娘淤堵。我一直安慰自己,他們只是感情好顷扩,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開白布拐邪。 她就那樣靜靜地躺著,像睡著了一般隘截。 火紅的嫁衣襯著肌膚如雪扎阶。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評(píng)論 1 305
  • 那天婶芭,我揣著相機(jī)與錄音东臀,去河邊找鬼。 笑死犀农,一個(gè)胖子當(dāng)著我的面吹牛惰赋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播呵哨,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼谤逼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了仇穗?” 一聲冷哼從身側(cè)響起流部,我...
    開封第一講書人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎纹坐,沒想到半個(gè)月后枝冀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年果漾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了球切。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绒障,死狀恐怖吨凑,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情户辱,我是刑警寧澤鸵钝,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站庐镐,受9級(jí)特大地震影響恩商,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜必逆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一怠堪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧名眉,春花似錦粟矿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至探橱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绘证,已是汗流浹背隧膏。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留嚷那,地道東北人胞枕。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像魏宽,于是被迫代替她去往敵國和親腐泻。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容