最近升級了iOS13系統(tǒng)摔认,之前項目使用系統(tǒng)TabBarController
創(chuàng)建的項目在啟動的時候selectTitleColor
和 NormalTitleColor
有時候設置不起作用。
tabBarItem
選中顏色有時為設置的顏色谎倔,有時變?yōu)橄到y(tǒng)藍色担忧;
tabBarItem
未選中顏色有時為設置的顏色,有時變?yōu)橄到y(tǒng)藍色截歉;
適配如下:
if (@available(iOS 13.0, *)) {
// iOS 13以上
self.tabBar.tintColor = [UIColor F2400BRedColor];
self.tabBar.unselectedItemTintColor = [UIColor grayColor];
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
[item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateSelected];
} else {
// iOS 13以下
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
[item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor F2400BRedColor]} forState:UIControlStateSelected];
}
查找資料過程中摇肌,發(fā)現(xiàn)還有以下設置也會起作用:
感謝博主「幻灬聽」
分享
原文鏈接:請點這里
Apple將設置UITabBarItem的文字屬性的任務,交給了13中新添加的屬性UITabBarItem.standardAppearance媒役。代碼如下:
viewController.tabBarItem.title = title;
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
// 設置未被選中的顏色
appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
// 設置被選中時的顏色
appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: :[UIColor F2400BRedColor]};
viewController.tabBarItem.standardAppearance = appearance;
} else {
[viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]}];
}
以上內容僅做為我自己的學習及收藏內容祝谚,如有不對請指出,Thanks?(?ω?)?~