253 lines
7.5 KiB
Objective-C
Executable File
253 lines
7.5 KiB
Objective-C
Executable File
//
|
|
// UINavigationBar+Background.m
|
|
// CustomNavicationController
|
|
//
|
|
// Created by YiJiang Chen on 15/10/22.
|
|
// Copyright (c) 2015年 YiJiang Chen. All rights reserved.
|
|
//
|
|
#import <objc/runtime.h>
|
|
#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 setClearNav];
|
|
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, -64, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds) + 64)];
|
|
} 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 setClearNav];
|
|
[self.overlay removeFromSuperview];
|
|
self.overlay = nil;
|
|
}
|
|
|
|
-(void)setClearNav
|
|
{
|
|
NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor whiteColor],
|
|
NSFontAttributeName : [UIFont systemFontOfSize:19]};
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
|
|
barApp.backgroundColor = [UIColor clearColor];
|
|
barApp.titleTextAttributes = dic;
|
|
barApp.backgroundEffect = nil;
|
|
barApp.shadowColor = nil;
|
|
self.scrollEdgeAppearance = nil;
|
|
self.standardAppearance = barApp;
|
|
}else{
|
|
self.titleTextAttributes = dic;
|
|
[self setShadowImage:[UIImage new]];
|
|
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
|
|
}
|
|
//透明
|
|
self.translucent = YES;
|
|
if (self.overlay) {
|
|
[self.overlay removeFromSuperview];
|
|
self.overlay = nil;
|
|
}
|
|
|
|
}
|
|
|
|
-(void)resetBackgroundImage{
|
|
|
|
|
|
[self setNavbgImage:[UIImage imageNamed:@"blackbar.png"]];
|
|
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
|
|
|
|
}
|
|
|
|
-(void)setWhiteNav
|
|
{
|
|
|
|
[self setNavbgImage:[UIImage imageNamed:@"blackbar_white.png"]];
|
|
|
|
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - 私有
|
|
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert
|
|
{
|
|
if ([stringToConvert hasPrefix:@"#"])
|
|
{
|
|
stringToConvert = [stringToConvert substringFromIndex:1];
|
|
}
|
|
float alpha=1.0;
|
|
if (stringToConvert.length>6&&![stringToConvert hasPrefix:@"0x"])//含有透明度
|
|
{
|
|
NSString*alphaString=[stringToConvert substringToIndex:2];
|
|
NSScanner *alphaScanner = [NSScanner scannerWithString:alphaString];
|
|
unsigned hex;
|
|
|
|
if (![alphaScanner scanHexInt:&hex])
|
|
{
|
|
alpha=1.0;
|
|
}
|
|
else
|
|
{
|
|
const char *hexChar = [alphaString cStringUsingEncoding:NSUTF8StringEncoding];
|
|
int hexNumber;
|
|
sscanf(hexChar, "%x", &hexNumber);
|
|
|
|
alpha=hexNumber/255.0;
|
|
|
|
}
|
|
}
|
|
|
|
NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
|
|
unsigned hexNum;
|
|
|
|
if (![scanner scanHexInt:&hexNum])
|
|
{
|
|
return [UIColor clearColor];
|
|
}
|
|
|
|
return [UIColor \
|
|
colorWithRed:((float)((hexNum & 0xFF0000) >> 16))/255.0 \
|
|
green:((float)((hexNum & 0xFF00) >> 8))/255.0 \
|
|
blue:((float)(hexNum & 0xFF))/255.0 alpha:alpha];
|
|
}
|
|
+ (UIImage *)createImageWithColor:(UIColor *)color
|
|
{
|
|
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
|
|
UIGraphicsBeginImageContext(rect.size);
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
|
CGContextSetFillColorWithColor(context, [color CGColor]);
|
|
CGContextFillRect(context, rect);
|
|
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
UIGraphicsEndImageContext();
|
|
return theImage;
|
|
}
|
|
|
|
- (void)setNavbgColorStr:(NSString *)colorStr
|
|
{
|
|
|
|
UIImage *image = [self.class createImageWithColor:[self.class colorWithHexString:colorStr]];
|
|
[self setNavbgImage:image];
|
|
}
|
|
- (void)setNavbgColor:(UIColor *)color
|
|
{
|
|
UIImage *image = [self.class createImageWithColor:color];
|
|
[self setNavbgImage:image];
|
|
}
|
|
- (void)setNavbgImage:(UIImage *)image
|
|
{
|
|
|
|
if (image) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:self];
|
|
apperar.backgroundImage = image;
|
|
apperar.backgroundImageContentMode=UIViewContentModeScaleAspectFill;
|
|
self.standardAppearance = apperar;
|
|
self.scrollEdgeAppearance = apperar;
|
|
}else{
|
|
[self setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
|
|
}
|
|
}
|
|
}
|
|
-(UIImage*)getNaveImage
|
|
{
|
|
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:self];
|
|
return apperar.backgroundImage;
|
|
}else{
|
|
|
|
return [self
|
|
backgroundImageForBarMetrics:UIBarMetricsDefault];
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
- (UINavigationBarAppearance *)getNavgationAppearanceWithNavgationController:(UINavigationBar *)bar
|
|
API_AVAILABLE(ios(13.0)){
|
|
if (@available(iOS 15.0, *)) {
|
|
if (bar.standardAppearance) {
|
|
return bar.standardAppearance;
|
|
}
|
|
|
|
if (bar.scrollEdgeAppearance) {
|
|
return bar.scrollEdgeAppearance;
|
|
}
|
|
|
|
return [[UINavigationBarAppearance alloc] init];
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
- (void)setNavTitleColor:(UIColor *)color
|
|
{
|
|
|
|
if (color ) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:self];
|
|
apperar.titleTextAttributes = @{NSForegroundColorAttributeName:color};
|
|
self.standardAppearance = apperar;
|
|
self.scrollEdgeAppearance = apperar;
|
|
}else{
|
|
self.titleTextAttributes = @{NSForegroundColorAttributeName:color};
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- (void)setNavTitleFont:(UIFont *)font
|
|
{
|
|
if (font ) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:self];
|
|
apperar.titleTextAttributes = @{NSFontAttributeName:font};
|
|
self.standardAppearance = apperar;
|
|
self.scrollEdgeAppearance = apperar;
|
|
}else{
|
|
self.titleTextAttributes = @{NSFontAttributeName:font};
|
|
}
|
|
|
|
}
|
|
}
|
|
- (void)setNavTitleTextAttributes:(NSDictionary*)attribute
|
|
{
|
|
|
|
if (attribute) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:self];
|
|
apperar.titleTextAttributes = attribute;
|
|
self.standardAppearance = apperar;
|
|
self.scrollEdgeAppearance = apperar;
|
|
}else{
|
|
self.titleTextAttributes = attribute;
|
|
}
|
|
|
|
}
|
|
}
|
|
@end
|