203 lines
6.1 KiB
Objective-C
203 lines
6.1 KiB
Objective-C
|
|
|
|
//
|
|
// UIViewController+Navgation.m
|
|
// E-Mobile7
|
|
//
|
|
// Created by ldg on 2018/4/13.
|
|
// Copyright © 2018年 Weaver. All rights reserved.
|
|
//
|
|
|
|
#import "UIViewController+Navgation.h"
|
|
|
|
|
|
@implementation UIViewController (Navgation)
|
|
|
|
|
|
+ (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
|
|
{
|
|
UINavigationController *nav = nil;
|
|
if ([self isKindOfClass:UINavigationController.class]) {
|
|
nav = (UINavigationController *)self;
|
|
}else{
|
|
nav = self.navigationController;
|
|
}
|
|
if (image && nav) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
|
|
apperar.backgroundImage = image;
|
|
nav.navigationBar.standardAppearance = apperar;
|
|
nav.navigationBar.scrollEdgeAppearance = apperar;
|
|
}else{
|
|
[nav.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
|
|
}
|
|
}
|
|
}
|
|
-(UIImage*)getNaveImage
|
|
{
|
|
UINavigationController *nav = nil;
|
|
if ([self isKindOfClass:UINavigationController.class]) {
|
|
nav = (UINavigationController *)self;
|
|
}else{
|
|
nav = self.navigationController;
|
|
}
|
|
if (nav) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
|
|
return apperar.backgroundImage;
|
|
}else{
|
|
|
|
return [nav.navigationBar
|
|
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
|
|
{
|
|
UINavigationController *nav = nil;
|
|
if ([self isKindOfClass:UINavigationController.class]) {
|
|
nav = (UINavigationController *)self;
|
|
}else{
|
|
nav = self.navigationController;
|
|
}
|
|
if (color && nav) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
|
|
apperar.titleTextAttributes = @{NSForegroundColorAttributeName:color};
|
|
nav.navigationBar.standardAppearance = apperar;
|
|
nav.navigationBar.scrollEdgeAppearance = apperar;
|
|
}else{
|
|
nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:color};
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- (void)setNavTitleFont:(UIFont *)font
|
|
{
|
|
UINavigationController *nav = nil;
|
|
if ([self isKindOfClass:UINavigationController.class]) {
|
|
nav = (UINavigationController *)self;
|
|
}else{
|
|
nav = self.navigationController;
|
|
}
|
|
if (font && nav) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
|
|
apperar.titleTextAttributes = @{NSFontAttributeName:font};
|
|
nav.navigationBar.standardAppearance = apperar;
|
|
nav.navigationBar.scrollEdgeAppearance = apperar;
|
|
}else{
|
|
nav.navigationBar.titleTextAttributes = @{NSFontAttributeName:font};
|
|
}
|
|
|
|
}
|
|
}
|
|
- (void)setNavTitleTextAttributes:(NSDictionary*)attribute
|
|
{
|
|
UINavigationController *nav = nil;
|
|
if ([self isKindOfClass:UINavigationController.class]) {
|
|
nav = (UINavigationController *)self;
|
|
}else{
|
|
nav = self.navigationController;
|
|
}
|
|
if (attribute && nav) {
|
|
if (@available(iOS 15.0, *)) {
|
|
UINavigationBarAppearance *apperar = [self getNavgationAppearanceWithNavgationController:nav.navigationBar];
|
|
apperar.titleTextAttributes = attribute;
|
|
nav.navigationBar.standardAppearance = apperar;
|
|
nav.navigationBar.scrollEdgeAppearance = apperar;
|
|
}else{
|
|
nav.navigationBar.titleTextAttributes = attribute;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@end
|