Skip to content

Commit

Permalink
配置类新增设置导航栏背景图片的属性
Browse files Browse the repository at this point in the history
  • Loading branch information
QuintGao committed Jun 25, 2021
1 parent d83d4f4 commit b919d27
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 31 deletions.
2 changes: 1 addition & 1 deletion GKNavigationBar.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 3 additions & 0 deletions GKNavigationBar/NavigationBar/GKNavigationBarConfigure.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions GKNavigationBarExample/GKNavigationBarExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "GKMainViewController.h"
#import "GKWYMusicViewController.h"
#import <TZImagePickerController/TZImagePickerController.h>
#import "UIView+Extension.h"

@interface AppDelegate ()

Expand All @@ -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];
// 导航栏标题颜色
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@

+ (instancetype)viewFromBgColor:(UIColor *)bgColor;

- (UIImage *)imageWithColors:(NSArray *)colors;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ configure.shiledGuestureVCs = @[NSClassFromString(@"TZPhotoPickerController"), @
<summary><font size=4>最近更新</font></summary>

```
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,手势处理优化
Expand Down

0 comments on commit b919d27

Please sign in to comment.