BaseNaviController.m 2.2 KB
//
//  BaseNaviController.m
//  DreamSleep
//
//  Created by peter on 2022/4/9.
//

#import "BaseNaviController.h"

@interface BaseNaviController ()
@end

@implementation BaseNaviController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航栏返回按钮颜色
    self.navigationBar.tintColor = DSWhite;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.childViewControllers.count == 1) {
        viewController.hidesBottomBarWhenPushed = YES;
    } else {
        viewController.hidesBottomBarWhenPushed = NO;
    }
    [super pushViewController:viewController animated:animated];
}

+ (void)setNavigationBarStyle:(NaviStyle)style vc:(UIViewController *)vc {
    UINavigationBar *naviBar = vc.navigationController.navigationBar;
    // NaviStyleDefault
    UIColor *titleColor = DSWhite;
    UIColor *bgColor = BrandColor;
    UIColor *barTintColor = BrandColor;
    if (style == NaviStyleLight) {
        titleColor = ColorFromHex(0x333333);
        bgColor = DSWhite;
        barTintColor = DSWhite;
    } else if (style == NaviStyleDark) {
        titleColor = DSWhite;
        bgColor = DarkColor;
        barTintColor = DarkColor;
    }
    
    // 设置导航栏title颜色、导航栏背景色、隐藏分割线
    NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName:titleColor};
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        // 导航栏title颜色
        appearance.titleTextAttributes = titleTextAttributes;
        // 导航栏背景色
        appearance.backgroundColor = bgColor;
        // 隐藏分割线
        appearance.shadowColor = DSClearColor;
        // 生效
        naviBar.scrollEdgeAppearance = appearance;
        // 滚动视图滚动后导航栏背景色不会被影响
        naviBar.standardAppearance = appearance;
    } else {
        // 导航栏title颜色
        [naviBar setTitleTextAttributes:titleTextAttributes];
        // 导航栏背景色
        naviBar.barTintColor = barTintColor;
        naviBar.translucent = NO;
        // 隐藏分割线
        naviBar.shadowImage = [UIImage new];
    }
}

@end