xcode13适配
This commit is contained in:
@@ -24,10 +24,10 @@ static char overlayKey;
|
||||
- (void)cnSetBackgroundColor:(UIColor *)backgroundColor
|
||||
{
|
||||
if (!self.overlay) {
|
||||
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
|
||||
[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, -44, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds) + 44)];
|
||||
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)];
|
||||
}
|
||||
@@ -42,17 +42,30 @@ static char overlayKey;
|
||||
- (void)cnReset
|
||||
{
|
||||
|
||||
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
|
||||
[self setClearNav];
|
||||
[self.overlay removeFromSuperview];
|
||||
self.overlay = nil;
|
||||
}
|
||||
|
||||
-(void)setClearNav
|
||||
{
|
||||
|
||||
[self setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
|
||||
//设置那条横线直接隐藏
|
||||
self.shadowImage = [[UIImage alloc] init];
|
||||
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;
|
||||
@@ -62,17 +75,178 @@ static char overlayKey;
|
||||
|
||||
-(void)resetBackgroundImage{
|
||||
|
||||
[self setBackgroundImage:[UIImage imageNamed:@"blackbar.png"] forBarMetrics:UIBarMetricsDefault];
|
||||
|
||||
[self setNavbgImage:[UIImage imageNamed:@"blackbar.png"]];
|
||||
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
|
||||
|
||||
}
|
||||
|
||||
-(void)setWhiteNav
|
||||
{
|
||||
[self setBackgroundImage:[UIImage imageNamed:@"blackbar_white.png"] forBarMetrics:UIBarMetricsDefault];
|
||||
|
||||
[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
|
||||
|
||||
Reference in New Issue
Block a user