問題
兼容性測(cè)試發(fā)現(xiàn) iOS 13.3的iPhone 8 Plus上食店,一個(gè)TabBar頁面底部BarItem文案變成省略號(hào)
影響范圍
設(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的尺寸不同
打印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;
}
}
}