JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
//  Copyright © 2019 dahua. All rights reserved.
//
 
#import "UIViewController+LCNavigationBar.h"
#import <LCBaseModule/UIFont+Dahua.h>
#import <objc/runtime.h>
#import "UIColor+LeChange.h"
 
static const void *KEY_DELEGATE = @"navigitionClickBlock";
static const void *KEY_RIGHTBTN = @"navigitionrightBtn";
 
@interface UIViewController ()
 
/// 回调代码
@property (copy, nonatomic) NavigationBtnClickBlock block;
 
/// 右侧按钮
@property (strong, nonatomic) LCButton *rightBtn;
 
/// 右侧按钮
//@property (copy, nonatomic) NavigationBtnClickBlock block;
 
@end
 
@implementation UIViewController (LCNavigationBar)
 
- (void)lcCreatNavigationBarWith:(LCNAVIGATION_STYLE)style buttonClickBlock:(NavigationBtnClickBlock)block {
    self.block = block;
    
    //创建返回按钮
    LCButton *backBtn = [LCButton lcButtonWithType:LCButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    backBtn.tag = 9000;
    [backBtn setImage:[UIImage imageNamed:(@"nav_back")] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(navigationBarClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem setLeftBarButtonItem:backItem];
    
    //创建闪关灯按钮
    LCButton *lightBtn = [LCButton lcButtonWithType:LCButtonTypeCustom];
    self.rightBtn = lightBtn;
    lightBtn.tag = 9001;
    [lightBtn setImage:[UIImage imageNamed:@"login_icon_user"] forState:UIControlStateNormal];
    lightBtn.hidden = YES;
    lightBtn.frame = CGRectMake(0, 0, 50, 50);
    [lightBtn addTarget:self action:@selector(navigationBarClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *lightItem = [[UIBarButtonItem alloc] initWithCustomView:lightBtn];
    [self.navigationItem setRightBarButtonItem:lightItem];
    
    //黑线
    [self.navigationController.navigationBar setHidden:NO];
    UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    //文字颜色(默认黑色)
    self.navigationController.navigationBar.titleTextAttributes = @{ NSFontAttributeName: [UIFont lcFont_t2], NSForegroundColorAttributeName: [UIColor dhcolor_c40] };
    //设置背景色
    [self.navigationController.navigationBar setTintColor:[UIColor dhcolor_c54]];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
 
    switch (style) {
        case LCNAVIGATION_STYLE_DEFAULT: {
//            navBarHairlineImageView.hidden = NO;
            backBtn.tintColor = [UIColor dhcolor_c60];
            [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        }
        break;
        case LCNAVIGATION_STYLE_CLEAR: {
//            白字体,白返回按钮 没有黑线
            navBarHairlineImageView.hidden = YES;
            self.navigationController.navigationBar.titleTextAttributes = @{ NSFontAttributeName: [UIFont lcFont_t2], NSForegroundColorAttributeName: [UIColor dhcolor_c43] };
            backBtn.tintColor = [UIColor dhcolor_c43];
            self.navigationController.navigationBar.backgroundColor = [UIColor dhcolor_c00];
            [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        }
        break;
        case LCNAVIGATION_STYLE_CLEARWITHLINE: {
            [self.navigationController.navigationBar setHidden:YES];
            CALayer *layer = [CALayer layer];
            layer.frame = CGRectMake(0, DH_NavViewHeight, SCREEN_WIDTH, 1);
            layer.backgroundColor = [UIColor dhcolor_c53].CGColor;
            [self.view.layer addSublayer:layer];
        }
        break;
        case LCNAVIGATION_STYLE_LIGHT: {
            lightBtn.hidden = NO;
            backBtn.tintColor = [UIColor dhcolor_c60];
            [lightBtn setImage:[UIImage imageNamed:(@"nav_flashlight_off")] forState:UIControlStateNormal];
        }
        break;
        case LCNAVIGATION_STYLE_DEVICELIST: {
            lightBtn.hidden = NO;
            backBtn.tintColor = [UIColor dhcolor_c60];
            [lightBtn setImage:[UIImage imageNamed:(@"common_icon_nav_adddevice")] forState:UIControlStateNormal];
        }
        break;
        case LCNAVIGATION_STYLE_LIVE: {
            lightBtn.hidden = NO;
            backBtn.tintColor = [UIColor dhcolor_c60];
            [lightBtn setImage:[UIImage imageNamed:(@"home_icon_device_setting")] forState:UIControlStateNormal];
        }
        break;
        case LCNAVIGATION_STYLE_CLEAR_YELLOW: {
            backBtn.tintColor = [UIColor dhcolor_c10];
            self.navigationController.navigationBar.titleTextAttributes = @{ NSFontAttributeName: [UIFont lcFont_t2], NSForegroundColorAttributeName: [UIColor dhcolor_c10] };
        }
        break;
        case LCNAVIGATION_STYLE_SUBMIT: {
            lightBtn.hidden = NO;
            backBtn.tintColor = [UIColor dhcolor_c60];
            [lightBtn setImage:[UIImage imageNamed:(@"setting_icon_check")] forState:UIControlStateNormal];
        }
        break;
 
        default:
            break;
    }
}
 
//MARK: - Private Methods
- (void)navigationBarClick:(LCButton *)items {
    switch (items.tag) {
        case 9000: {
            if (self.block) {
                self.block(0);
            } else {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
        break;
        case 9001: {
            if (self.block) {
                self.block(1);
            }
        }
        break;
 
        default:
 
            break;
    }
}
 
- (LCButton *)rightBtn {
    id obj = objc_getAssociatedObject(self, KEY_RIGHTBTN);
    return obj;
}
 
- (void)setRightBtn:(LCButton *)rightBtn {
    objc_setAssociatedObject(self, KEY_RIGHTBTN, rightBtn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (void)setBlock:(NavigationBtnClickBlock)block {
    objc_setAssociatedObject(self, KEY_DELEGATE, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
 
- (NavigationBtnClickBlock)block {
    id obj = objc_getAssociatedObject(self, KEY_DELEGATE);
    if (obj) {
        return (NavigationBtnClickBlock)obj;
    }
    return nil;
}
 
//通过一个方法来找到这个黑线(findHairlineImageViewUnder):
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
 
- (LCButton *)lc_getRightBtn {
    NSLog(@"%@", self.rightBtn);
    return self.rightBtn;
}
 
@end