diff --git a/GKNavigationBar.podspec b/GKNavigationBar.podspec index 28ed6d7..c89ffd1 100644 --- a/GKNavigationBar.podspec +++ b/GKNavigationBar.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GKNavigationBar' - s.version = '1.5.8' + s.version = '1.5.9' s.license = 'MIT' s.summary = '自定义导航栏--导航栏联动' s.homepage = 'https://github.com/QuintGao/GKNavigationBar' diff --git a/GKNavigationBar/NavigationBar/GKNavigationBarConfigure.h b/GKNavigationBar/NavigationBar/GKNavigationBarConfigure.h index c657bd6..5a27e23 100644 --- a/GKNavigationBar/NavigationBar/GKNavigationBarConfigure.h +++ b/GKNavigationBar/NavigationBar/GKNavigationBarConfigure.h @@ -19,6 +19,9 @@ NS_ASSUME_NONNULL_BEGIN /// 导航栏背景色,默认白色 @property (nonatomic, strong) UIColor *backgroundColor; +/// 导航栏背景色,默认nil,优先级高于backgroundColor +@property (nonatomic, strong) UIImage *backgroundImage; + /// 导航栏分割线背景色,默认nil,使用系统颜色 @property (nonatomic, strong) UIColor *lineColor; diff --git a/GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.h b/GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.h index 28065ff..bb71fe6 100644 --- a/GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.h +++ b/GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.h @@ -52,9 +52,9 @@ NS_ASSUME_NONNULL_BEGIN /// 设置返回按钮类型 @property (nonatomic, assign) GKNavigationBarBackStyle gk_backStyle; -/// 导航栏背景 -@property (nonatomic, strong) UIColor *gk_navBackgroundColor; +/// 导航栏背景,gk_navBackgroundImage优先级较高 @property (nonatomic, strong) UIImage *gk_navBackgroundImage; +@property (nonatomic, strong) UIColor *gk_navBackgroundColor; /// 导航栏分割线 @property (nonatomic, strong) UIColor *gk_navShadowColor; diff --git a/GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.m b/GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.m index 93a414e..fa6d358 100644 --- a/GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.m +++ b/GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.m @@ -278,7 +278,7 @@ - (GKNavigationBarBackStyle)gk_backStyle { static char kAssociatedObjectKey_navBackgroundColor; - (void)setGk_navBackgroundColor:(UIColor *)gk_navBackgroundColor { objc_setAssociatedObject(self, &kAssociatedObjectKey_navBackgroundColor, gk_navBackgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); - + [self setNavBackgroundColor:gk_navBackgroundColor]; } @@ -565,8 +565,12 @@ - (BOOL)canFindPresentedViewController:(UIViewController *)viewController { #pragma mark - Private Methods - (void)setupNavBarAppearance { - // 设置默认背景色 - if (self.gk_navBackgroundColor == nil) { + // 设置默认背景 + if (self.gk_navBackgroundImage == nil) { + self.gk_navBackgroundImage = GKConfigure.backgroundImage; + } + + if (self.gk_navBackgroundColor == nil && self.gk_navBackgroundImage == nil) { self.gk_navBackgroundColor = GKConfigure.backgroundColor; } diff --git a/GKNavigationBarExample/GKNavigationBarExample/AppDelegate.m b/GKNavigationBarExample/GKNavigationBarExample/AppDelegate.m index 2d33a6b..3869055 100644 --- a/GKNavigationBarExample/GKNavigationBarExample/AppDelegate.m +++ b/GKNavigationBarExample/GKNavigationBarExample/AppDelegate.m @@ -10,6 +10,7 @@ #import "GKMainViewController.h" #import "GKWYMusicViewController.h" #import +#import "UIView+Extension.h" @interface AppDelegate () @@ -24,6 +25,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( // NSLog(@"%@", NSStringFromUIEdgeInsets([GKConfigure gk_safeAreaInsets])); // 配置导航栏属性 [GKConfigure setupCustomConfigure:^(GKNavigationBarConfigure * _Nonnull configure) { + UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, GK_STATUSBAR_NAVBAR_HEIGHT)]; + configure.backgroundImage = [view imageWithColors:@[(__bridge id)[UIColor colorWithRed:127/255.0 green:23/255.0 blue:135/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:37/255.0 green:26/255.0 blue:188/255.0 alpha:1.0].CGColor]]; // 导航栏背景色 configure.backgroundColor = [UIColor whiteColor]; // 导航栏标题颜色 diff --git a/GKNavigationBarExample/GKNavigationBarExample/Demo/BaseVC/UIView+Extension.h b/GKNavigationBarExample/GKNavigationBarExample/Demo/BaseVC/UIView+Extension.h index b9a69cf..274b30b 100755 --- a/GKNavigationBarExample/GKNavigationBarExample/Demo/BaseVC/UIView+Extension.h +++ b/GKNavigationBarExample/GKNavigationBarExample/Demo/BaseVC/UIView+Extension.h @@ -30,4 +30,6 @@ + (instancetype)viewFromBgColor:(UIColor *)bgColor; +- (UIImage *)imageWithColors:(NSArray *)colors; + @end diff --git a/GKNavigationBarExample/GKNavigationBarExample/Demo/BaseVC/UIView+Extension.m b/GKNavigationBarExample/GKNavigationBarExample/Demo/BaseVC/UIView+Extension.m index 3598d72..ee0b764 100755 --- a/GKNavigationBarExample/GKNavigationBarExample/Demo/BaseVC/UIView+Extension.m +++ b/GKNavigationBarExample/GKNavigationBarExample/Demo/BaseVC/UIView+Extension.m @@ -145,4 +145,31 @@ + (instancetype)viewFromBgColor:(UIColor *)bgColor return view; } +- (UIImage *)imageWithColors:(NSArray *)colors { + [self addGradualLayerWithColors:colors]; + return [self convertToImage]; +} + +- (void)addGradualLayerWithColors:(NSArray *)colors { + if (colors == nil && colors.count == 0) return; + + CAGradientLayer *gradientLayer = [CAGradientLayer layer]; + gradientLayer.colors = colors; + gradientLayer.locations = @[@0, @1.0]; + gradientLayer.startPoint = CGPointMake(0.02, 0.5); + gradientLayer.endPoint = CGPointMake(1.0, 0.5); + gradientLayer.frame = self.bounds; + [self.layer addSublayer:gradientLayer]; +} + +- (UIImage *)convertToImage { + CGSize s = self.bounds.size; + // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了 + UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale); + [self.layer renderInContext:UIGraphicsGetCurrentContext()]; + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return image; +} + @end diff --git a/GKNavigationBarExample/GKNavigationBarExample/Demo/DemoVC/GKDemo000ViewController.m b/GKNavigationBarExample/GKNavigationBarExample/Demo/DemoVC/GKDemo000ViewController.m index f895f26..2471555 100644 --- a/GKNavigationBarExample/GKNavigationBarExample/Demo/DemoVC/GKDemo000ViewController.m +++ b/GKNavigationBarExample/GKNavigationBarExample/Demo/DemoVC/GKDemo000ViewController.m @@ -66,6 +66,26 @@ - (void)viewDidLoad { self.gk_navItemRightSpace = 30; self.gk_navRightBarButtonItem = self.moreItem; + if (@available(iOS 13.0, *)) { + self.gk_navBackgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) { + if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { + return UIColor.whiteColor; + }else { + return UIColor.redColor; + } + }]; + self.gk_navTitleColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) { + if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { + return UIColor.blackColor; + }else { + return UIColor.whiteColor; + } + }]; + }else { + self.gk_navBackgroundColor = UIColor.redColor; + self.gk_navTitleColor = [UIColor whiteColor]; + } + self.leftPushSwitch.on = NO; self.moreItemSwitch.on = NO; self.fullScreenInterceptSwitch.on = NO; diff --git a/GKNavigationBarExample/GKNavigationBarExample/Demo/GKMainViewController.m b/GKNavigationBarExample/GKNavigationBarExample/Demo/GKMainViewController.m index 8896981..68464dd 100644 --- a/GKNavigationBarExample/GKNavigationBarExample/Demo/GKMainViewController.m +++ b/GKNavigationBarExample/GKNavigationBarExample/Demo/GKMainViewController.m @@ -56,31 +56,7 @@ - (void)viewDidLoad { self.gk_navigationItem.title = @"MainVC"; self.gk_statusBarStyle = UIStatusBarStyleLightContent; self.gk_navTitleFont = [UIFont systemFontOfSize:18.0f]; - - if (@available(iOS 13.0, *)) { - self.gk_navTitleColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) { - if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { - return UIColor.blackColor; - }else { - return UIColor.whiteColor; - } - }]; - }else { - self.gk_navTitleColor = [UIColor whiteColor]; - } - - if (@available(iOS 13.0, *)) { - self.gk_navBackgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) { - if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { - return UIColor.whiteColor; - }else { - return UIColor.redColor; - } - }]; - } else { - // Fallback on earlier versions - self.gk_navBackgroundColor = UIColor.redColor; - } + self.gk_navTitleColor = UIColor.whiteColor; [self setupTableView]; } diff --git a/README.md b/README.md index a66b792..25e8a4d 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,7 @@ configure.shiledGuestureVCs = @[NSClassFromString(@"TZPhotoPickerController"), @ 最近更新 ``` +1.5.9 - 2021.06.25 配置类增加backgroundImage,可设置全局导航图片 1.5.8 - 2021.06.09 增加恢复系统导航栏显示的逻辑及方法 #86 1.5.7 - 2021.05.20 修复bug #78,创建导航栏item方法优化 1.5.6 - 2021.05.18 优化导航栏返回按钮及高度处理#77,手势处理优化