iOS 13 Tabbar 選中push返回文字渲染返回藍色 和去除上面黑線

一、設置文字的顏色 

如果在iOS13中使用以下代碼設置Tabbar文字渲染在點擊其他item切換或則push返回的時候是會失效的,字體顏色是會返回系統默認的藍色

    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

iOS13後文檔中對#UIBbarAppearance#對象有說明,在設置的對象屬性時需要初始化並設置一個UIBarAppearance或則子類來設置其對象屬性

正確設置方式:

if (@available(iOS 13.0, *)) {
        // titColor就是選中的顏色
        self.tabBar.tintColor = [UIColor redColor];
  //如果需要設置默認顏色可以使用setUnselectedItemTintColor來設置未選中顏色
       [self.tabBar setUnselectedItemTintColor:UIColor redColor];
    } else {
        
    // 統一給所有的UITabBarItem設置文字屬性
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    }

二、去除上面的線

if (@available(iOS 13.0,*)) {
            
            UITabBarAppearance * appeearance = self.standardAppearance;
            appeearance.backgroundImage = [UIImage imageWithColor:[UIColor clearColor]];;
            appeearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];;
            self.standardAppearance = appeearance;
            
        }
        else{
            self.backgroundImage = [UIImage new];
            self.shadowImage = [UIImage new];
        }
        
        //imageWithColor是爲UIImage寫的一個分類方法:根據顏色生成一張圖片,其中 [UIImage new] 方法已不在適用。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章