// // UINavigationBar+Background.m // CustomNavicationController // // Created by YiJiang Chen on 15/10/22. // Copyright (c) 2015年 YiJiang Chen. All rights reserved. // #import #import "UINavigationBar+Background.h" @implementation UINavigationBar (Background) static char overlayKey; - (UIView *)overlay { return objc_getAssociatedObject(self, &overlayKey); } - (void)setOverlay:(UIView *)overlay { objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)cnSetBackgroundColor:(UIColor *)backgroundColor { if (!self.overlay) { [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds) + 20)]; if (@available(iOS 11.0, *)) { self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -44, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds) + 44)]; } else { self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds) + 20)]; } self.overlay.userInteractionEnabled = NO; self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; [self insertSubview:self.overlay atIndex:0]; } self.overlay.backgroundColor = backgroundColor; } - (void)cnReset { [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [self.overlay removeFromSuperview]; self.overlay = nil; } -(void)setClearNav { [self setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; //设置那条横线直接隐藏 self.shadowImage = [[UIImage alloc] init]; if (self.overlay) { [self.overlay removeFromSuperview]; self.overlay = nil; } } -(void)resetBackgroundImage{ [self setBackgroundImage:[UIImage imageNamed:@"blackbar.png"] forBarMetrics:UIBarMetricsDefault]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO]; } -(void)setWhiteNav { [self setBackgroundImage:[UIImage imageNamed:@"blackbar_white.png"] forBarMetrics:UIBarMetricsDefault]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO]; } @end