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)

// 根据颜色生成图片
+ (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;
}

你可能感兴趣的:(iOS适配iOS13、iOS15的UITabBar和UINavigationBar)