iOS適配iOS13夏跷、iOS15的UITabBar和UINavigationBar

1.適配UITabBar

- (void)setupTabBar
{
    UIColor *backgroundColor = [UIColor whiteColor];
    UIColor *shadowColor = UIColorFromHEX(0xE5E5E5);
    
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        appearance.backgroundColor = backgroundColor;
        appearance.shadowColor = shadowColor;
        appearance.backgroundEffect = nil;
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10], NSForegroundColorAttributeName:kColor999999};
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10], NSForegroundColorAttributeName:kColorTheme};
        self.tabBar.standardAppearance = appearance;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = appearance;
        }
    } else {
        UIImage *shadowImage = [Utils imageWithColor:shadowColor size:CGSizeMake(SCREEN_WIDTH, 0.5f)];
        UIImage *backgroundImage = [Utils imageWithColor:backgroundColor size:CGSizeMake(SCREEN_WIDTH, kTabbarHeight)];
        [self.tabBar setShadowImage:shadowImage];
        [self.tabBar setBackgroundImage:backgroundImage];
    }
}

2.適配UINavigationBar

- (void)setupNavigateionBar
{
    UIColor *backgroundColor = [UIColor whiteColor];
    UIColor *shadowColor = [UIColor whiteColor];
    NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor],
                                          NSFontAttributeName: [UIFont systemFontOfSize:18 weight:UIFontWeightBold]};
    
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        appearance.backgroundColor = backgroundColor;
        appearance.shadowColor = shadowColor;
        appearance.titleTextAttributes = titleTextAttributes;
        self.navigationBar.standardAppearance = appearance;
        if (@available(iOS 15.0, *)) {
            self.navigationBar.scrollEdgeAppearance = appearance;
        }
    }else{
        self.navigationBar.barTintColor = backgroundColor;
        self.navigationBar.titleTextAttributes = titleTextAttributes;
        [self.navigationBar setShadowImage:[Utils imageWithColor:shadowColor size:CGSizeMake(SCREEN_WIDTH, 0.5f)]];
        [self.navigationBar setBackgroundImage:[Utils imageWithColor:backgroundColor size:CGSizeMake(SCREEN_WIDTH, kStatusBarAndNavigationBarHeight)] forBarMetrics:UIBarMetricsDefault];
    }
    // 不透明
    self.navigationBar.translucent = NO;
    // navigation控件顏色
    self.navigationBar.tintColor = [UIColor blackColor];

}

其中用到的宏和工具方法

// 16進制顏色
#define UIColorFromHEX(rgbValue)    [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// 設備寬度
#define SCREEN_WIDTH    [UIScreen mainScreen].bounds.size.width
// 判斷是否異性全面屏
#define kIsFullScreen ({\
BOOL isBangsScreen = NO; \
if (@available(iOS 11.0, *)) { \
UIWindow *window = [[UIApplication sharedApplication].windows firstObject]; \
isBangsScreen = window.safeAreaInsets.bottom > 0; \
} \
isBangsScreen; \
})
// Tabbar height.
#define kTabbarHeight (kIsFullScreen ? (49.f+34.f) : 49.f)
// Status bar & navigation bar height.
#define kStatusBarAndNavigationBarHeight (kIsFullScreen ? 88.f : 64.f)

// 根據(jù)顏色生成圖片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市矢沿,隨后出現(xiàn)的幾起案子议街,更是在濱河造成了極大的恐慌,老刑警劉巖碧囊,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異纤怒,居然都是意外死亡糯而,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門泊窘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來熄驼,“玉大人,你說我怎么就攤上這事烘豹」霞郑” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵携悯,是天一觀的道長阐虚。 經常有香客問我,道長蚌卤,這世上最難降的妖魔是什么实束? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮逊彭,結果婚禮上咸灿,老公的妹妹穿的比我還像新娘。我一直安慰自己侮叮,他們只是感情好避矢,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著囊榜,像睡著了一般审胸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上卸勺,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天砂沛,我揣著相機與錄音,去河邊找鬼曙求。 笑死碍庵,一個胖子當著我的面吹牛,可吹牛的內容都是我干的悟狱。 我是一名探鬼主播静浴,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼挤渐!你這毒婦竟也來了苹享?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤浴麻,失蹤者是張志新(化名)和其女友劉穎得问,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體白胀,經...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡椭赋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了或杠。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哪怔。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖向抢,靈堂內的尸體忽然破棺而出认境,到底是詐尸還是另有隱情,我是刑警寧澤挟鸠,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布叉信,位于F島的核電站,受9級特大地震影響艘希,放射性物質發(fā)生泄漏硼身。R本人自食惡果不足惜硅急,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望佳遂。 院中可真熱鬧营袜,春花似錦、人聲如沸丑罪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吩屹。三九已至跪另,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間煤搜,已是汗流浹背免绿。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留宅楞,地道東北人针姿。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像厌衙,于是被迫代替她去往敵國和親距淫。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內容