JLChen
2021-05-17 a722e767f98042d5ef6259d2dde7854c925e4167
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
//  Copyright © 2019 jm. All rights reserved.
//
 
#import "DHNavBarPresenter.h"
#import <Masonry/Masonry.h>
#import <LCBaseModule/LCBaseModule.h>
#import "DHContainerVC.h"
@implementation DHNavBarPresenter
{
    UIView *_lineView;
    UIView *_shadowImageView;
}
 
- (instancetype)init {
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleDeviceOrientationDidChange:)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:nil];
    }
    return self;
}
 
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]  removeObserver:self];
}
 
#pragma mark - 懒加载
- (DHContainerVC *)containerVC {
    if ([self.viewController.parentViewController isKindOfClass:[DHContainerVC class]]) {
        return (DHContainerVC *)self.viewController.parentViewController;
    }
    return nil;
}
 
- (UINavigationItem *)customNavigationItem {
    if (!_customNavigationItem) {
        _customNavigationItem = [[DHNavigationItem alloc]init];
    }
    return _customNavigationItem;
}
 
- (UINavigationBar *)customNavigationBar {
    if (!_customNavigationBar) {
        _customNavigationBar = [[UINavigationBar alloc]init];
        _customNavigationBar.barTintColor = [UIColor dhcolor_c43];
        _customNavigationBar.items = @[self.customNavigationItem];
        _lineView = [[UIView alloc]init];
        [_customNavigationBar addSubview:_lineView];
        _lineView.backgroundColor = [UIColor dhcolor_c8];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0.0);
            make.height.mas_equalTo(0.5);
            make.left.right.mas_equalTo(0.0);
        }];
        
    }
    return _customNavigationBar;
}
 
- (void)viewDidLoad {
    _shadowImageView = [self findShadowImage:self.customNavigationBar];
    
    //默认隐藏导航栏底部的线
    [self setIsBarShadowHidden:YES];
    
    //如果是从nib或者storyboard中加载的  那么将导航栏上的按钮迁移过来
    if (self.viewController.nibBundle || self.viewController.storyboard) {
        self.customNavigationItem.rightBarButtonItems = self.viewController.superNavigationItem.rightBarButtonItems;
        self.customNavigationItem.title = self.viewController.title;
        
        self.customNavigationItem.titleView = self.viewController.superNavigationItem.titleView;
        
    }
    
    //设置返回按钮
    //如果是导航栈的第一个视图  那么不显示返回按钮
    //否则显示返回按钮
    if (self.viewController.navigationController.viewControllers.firstObject != self.viewController &&
        self.customNavigationItem.leftBarButtonItems.count == 0 && [self.viewController respondsToSelector:@selector(onLeftNaviItemClick:)]) {
        //使用原始渲染、图片向左等偏移处理
        UIImage *image = [UIImage imageNamed:@"nav_back"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self.viewController action:@selector(onLeftNaviItemClick:)];
//        item.imageInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        self.customNavigationItem.leftBarButtonItems = @[item];
    }
}
 
- (void)viewDidAppear {
    /*
     这有一个问题  如果前面一个页面是横屏  然后跳转到后面一个页面是竖屏  那么状态栏的frame的变化通知会出现问题 frame不准确
     所以放在viewwillAppera这里进行兼容    下面去掉的话  导航栏会往上面跑 被状态栏压住
     */
    
    // LEAK: superview == baseController.view 会造成循环引用
    __weak UIView *superView = self.customNavigationBar.superview;
    if (superView == nil) {
        return;
    }
    self.customNavigationBar.hidden = self.navigationBarHidden;
    CGFloat height = UIApplication.sharedApplication.statusBarFrame.size.height;
    if (height != self.customNavigationBar.frame.origin.y) {
        CGFloat desAlpha = self.navigationBarHidden ? 0.0 : 1.0;
        CGFloat desHeight = self.navigationBarHidden ?  0.0 : 44.0;
        CGFloat desTop = self.navigationBarHidden ? 0 : height;
        [self.customNavigationBar mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(superView).offset(desTop);
            make.left.mas_equalTo(superView);
            make.right.mas_equalTo(superView);
            make.height.mas_equalTo(desHeight);
        }];
 
        [UIView animateWithDuration:0.1 animations:^{
            [superView layoutIfNeeded];
            self.customNavigationBar.alpha = desAlpha;
        }];
    }
}
 
- (void)viewWillTransitionToSize {
    [self viewDidAppear];
}
 
#pragma mark - 阴影线
- (void)setIsBarShadowHidden:(BOOL)isBarShadowHidden {
    _isBarShadowHidden = isBarShadowHidden;
    if (_shadowImageView) {
        _shadowImageView.hidden = isBarShadowHidden;
         _lineView.hidden = isBarShadowHidden;
    }
}
 
#pragma mark - 屏幕旋转通知回调
- (void)handleDeviceOrientationDidChange:(NSNotification *)notification {
 
    //NSLog(@"handleDeviceOrientationDidChange %@", [UIApplication sharedApplication].statusBarHidden ? @"YES" : @"NO");
    // LEAK: superview == baseController.view 会造成循环引用
    __weak UIView *superView = self.customNavigationBar.superview;
    if (superView == nil) {
        return ;
    }
    
    CGFloat height = UIApplication.sharedApplication.statusBarFrame.size.height;
    if (height != self.customNavigationBar.frame.origin.y) {
        CGFloat desAlpha = self.navigationBarHidden ? 0.0 : 1.0;
        CGFloat desHeight = self.navigationBarHidden ?  0.0 : 44.0;
        CGFloat desTop = self.navigationBarHidden ? 0 : height;
        [self.customNavigationBar mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(superView).offset(desTop);
            make.left.equalTo(superView);
            make.right.equalTo(superView);
            make.height.mas_equalTo(desHeight);
        }];
        
        [UIView animateWithDuration:0.1 animations:^{
            [superView layoutIfNeeded];
            self.customNavigationBar.alpha = desAlpha;
        }];
    }
}
 
 
- (void)setNavigationBarHidden:(BOOL)navigationBarHidden {
    [self setNavigationBarHidden:navigationBarHidden animated:NO];
}
 
- (void)setNavigationBarHidden:(BOOL)navigationBarHidden animated:(BOOL)animated {
    _navigationBarHidden = navigationBarHidden;
    // LEAK: superview == baseController.view 会造成循环引用
    __weak UIView *superView = self.customNavigationBar.superview;
    if(superView == nil) {
        return ;
    }
 
    CGFloat desHeight = self.navigationBarHidden ?  0.0 : 44.0;
    CGFloat desAlpha = self.navigationBarHidden ? 0.0 : 1.0;
    CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
    CGFloat desTop = self.navigationBarHidden ? 0 : statusRect.size.height;
    [self.customNavigationBar mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(desTop);
        make.left.mas_equalTo(0.0);
        make.right.mas_equalTo(0.0);
        make.height.mas_equalTo(desHeight);
    }];
 
    if (animated) {
        [UIView animateWithDuration:0.3 animations:^{
            [superView layoutIfNeeded];
            self.customNavigationBar.alpha = desAlpha;
        }];
    } else {
        self.customNavigationBar.alpha = desAlpha;
    }
}
 
#pragma mark - Shadow imageview
- (UIView *)findShadowImage:(UIView *)view {
    //【*】为什么找不到UIImageView??
    if (view.bounds.size.height <= 1) {
        return view;
    }
    
    for (UIView *subview in view.subviews) {
        UIView *view = [self findShadowImage:subview];
        if (view != nil) {
            return view;
        }
    }
    return nil;
}
 
 
@end