杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
 * Tencent is pleased to support the open source community by making QMUI_iOS available.
 * Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
 
//
//  QMUICommonViewController.m
//  qmui
//
//  Created by QMUI Team on 14-6-22.
//
 
#import "QMUICommonViewController.h"
#import "QMUICore.h"
#import "QMUINavigationTitleView.h"
#import "QMUIEmptyView.h"
#import "NSString+QMUI.h"
#import "NSObject+QMUI.h"
#import "UIViewController+QMUI.h"
#import "UIGestureRecognizer+QMUI.h"
#import "UIView+QMUI.h"
 
@interface QMUIViewControllerHideKeyboardDelegateObject : NSObject <UIGestureRecognizerDelegate, QMUIKeyboardManagerDelegate>
 
@property(nonatomic, weak) QMUICommonViewController *viewController;
 
- (instancetype)initWithViewController:(QMUICommonViewController *)viewController;
@end
 
@interface QMUICommonViewController () {
    UITapGestureRecognizer *_hideKeyboardTapGestureRecognizer;
    QMUIKeyboardManager *_hideKeyboardManager;
    QMUIViewControllerHideKeyboardDelegateObject *_hideKeyboadDelegateObject;
}
 
@property(nonatomic,strong,readwrite) QMUINavigationTitleView *titleView;
@end
 
@implementation QMUICommonViewController
 
#pragma mark - 生命周期
 
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self didInitialize];
    }
    return self;
}
 
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self didInitialize];
    }
    return self;
}
 
- (void)didInitialize {
    self.titleView = [[QMUINavigationTitleView alloc] init];
    self.titleView.title = self.title;// 从 storyboard 初始化的话,可能带有 self.title 的值
    self.navigationItem.titleView = self.titleView;
    
    // 不管navigationBar的backgroundImage如何设置,都让布局撑到屏幕顶部,方便布局的统一
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    self.supportedOrientationMask = SupportedOrientationMask;
    
    if (QMUICMIActivated) {
        self.hidesBottomBarWhenPushed = HidesBottomBarWhenPushedInitially;
        self.qmui_preferredStatusBarStyleBlock = ^UIStatusBarStyle{
            return DefaultStatusBarStyle;
        };
    }
    
    self.qmui_prefersHomeIndicatorAutoHiddenBlock = ^BOOL{
        return NO;
    };
 
    
    // 动态字体notification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentSizeCategoryDidChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    if (!self.view.backgroundColor && QMUICMIActivated) {// nib 里可能设置了,所以做个 if 的判断
        self.view.backgroundColor = UIColorForBackground;
    }
    
    // 点击空白区域降下键盘 QMUICommonViewController (QMUIKeyboard)
    // 如果子类重写了才初始化这些对象(即便子类 return NO)
    BOOL shouldEnabledKeyboardObject = [self qmui_hasOverrideMethod:@selector(shouldHideKeyboardWhenTouchInView:) ofSuperclass:[QMUICommonViewController class]];
    if (shouldEnabledKeyboardObject) {
        _hideKeyboadDelegateObject = [[QMUIViewControllerHideKeyboardDelegateObject alloc] initWithViewController:self];
        
        _hideKeyboardTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:nil action:NULL];
        self.hideKeyboardTapGestureRecognizer.delegate = _hideKeyboadDelegateObject;
        self.hideKeyboardTapGestureRecognizer.enabled = NO;
        [self.view addGestureRecognizer:self.hideKeyboardTapGestureRecognizer];
        
        _hideKeyboardManager = [[QMUIKeyboardManager alloc] initWithDelegate:_hideKeyboadDelegateObject];
    }
    
    [self initSubviews];
}
 
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // fix iOS 11 and later, shouldHideKeyboardWhenTouchInView: will not work when calling becomeFirstResponder in UINavigationController.rootViewController.viewDidLoad
    // https://github.com/Tencent/QMUI_iOS/issues/495
    if (self.hideKeyboardManager && [QMUIKeyboardManager isKeyboardVisible]) {
        self.hideKeyboardTapGestureRecognizer.enabled = YES;
    }
}
 
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self layoutEmptyView];
}
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setupNavigationItems];
    [self setupToolbarItems];
}
 
#pragma mark - 空列表视图 QMUIEmptyView
 
@synthesize emptyView = _emptyView;
 
- (QMUIEmptyView *)emptyView {
    if (!_emptyView && self.isViewLoaded) {
        _emptyView = [[QMUIEmptyView alloc] initWithFrame:self.view.bounds];
    }
    return _emptyView;
}
 
- (void)showEmptyView {
    [self.view addSubview:self.emptyView];
}
 
- (void)hideEmptyView {
    [_emptyView removeFromSuperview];
}
 
- (BOOL)isEmptyViewShowing {
    return _emptyView && _emptyView.superview;
}
 
- (void)showEmptyViewWithLoading {
    [self showEmptyView];
    [self.emptyView setImage:nil];
    [self.emptyView setLoadingViewHidden:NO];
    [self.emptyView setTextLabelText:nil];
    [self.emptyView setDetailTextLabelText:nil];
    [self.emptyView setActionButtonTitle:nil];
}
 
- (void)showEmptyViewWithText:(NSString *)text
                   detailText:(NSString *)detailText
                  buttonTitle:(NSString *)buttonTitle
                 buttonAction:(SEL)action {
    [self showEmptyViewWithLoading:NO image:nil text:text detailText:detailText buttonTitle:buttonTitle buttonAction:action];
}
 
- (void)showEmptyViewWithImage:(UIImage *)image
                          text:(NSString *)text
                    detailText:(NSString *)detailText
                   buttonTitle:(NSString *)buttonTitle
                  buttonAction:(SEL)action {
    [self showEmptyViewWithLoading:NO image:image text:text detailText:detailText buttonTitle:buttonTitle buttonAction:action];
}
 
- (void)showEmptyViewWithLoading:(BOOL)showLoading
                           image:(UIImage *)image
                            text:(NSString *)text
                      detailText:(NSString *)detailText
                     buttonTitle:(NSString *)buttonTitle
                    buttonAction:(SEL)action {
    [self showEmptyView];
    [self.emptyView setLoadingViewHidden:!showLoading];
    [self.emptyView setImage:image];
    [self.emptyView setTextLabelText:text];
    [self.emptyView setDetailTextLabelText:detailText];
    [self.emptyView setActionButtonTitle:buttonTitle];
    [self.emptyView.actionButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
    [self.emptyView.actionButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
}
 
- (BOOL)layoutEmptyView {
    if (_emptyView) {
        // 由于为self.emptyView设置frame时会调用到self.view,为了避免导致viewDidLoad提前触发,这里需要判断一下self.view是否已经被初始化
        BOOL viewDidLoad = self.emptyView.superview && [self isViewLoaded];
        if (viewDidLoad) {
            CGSize newEmptyViewSize = self.emptyView.superview.bounds.size;
            CGSize oldEmptyViewSize = self.emptyView.frame.size;
            if (!CGSizeEqualToSize(newEmptyViewSize, oldEmptyViewSize)) {
                self.emptyView.qmui_frameApplyTransform = CGRectFlatMake(CGRectGetMinX(self.emptyView.frame), CGRectGetMinY(self.emptyView.frame), newEmptyViewSize.width, newEmptyViewSize.height);
            }
            return YES;
        }
    }
    
    return NO;
}
 
#pragma mark - 屏幕旋转
 
- (BOOL)shouldAutorotate {
    return YES;
}
 
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.supportedOrientationMask;
}
 
@end
 
@implementation QMUICommonViewController (QMUISubclassingHooks)
 
- (void)initSubviews {
    // 子类重写
}
 
- (void)setupNavigationItems {
    // 子类重写
}
 
- (void)setupToolbarItems {
    // 子类重写
}
 
- (void)contentSizeCategoryDidChanged:(NSNotification *)notification {
    // 子类重写
}
 
@end
 
@implementation QMUICommonViewController (QMUINavigationController)
 
- (void)updateNavigationBarAppearance {
    
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    if (!navigationBar) return;
    
    if ([self respondsToSelector:@selector(qmui_navigationBarBackgroundImage)]) {
        [navigationBar setBackgroundImage:[self qmui_navigationBarBackgroundImage] forBarMetrics:UIBarMetricsDefault];
    }
    if ([self respondsToSelector:@selector(qmui_navigationBarBarTintColor)]) {
        navigationBar.barTintColor = [self qmui_navigationBarBarTintColor];
    }
    if ([self respondsToSelector:@selector(qmui_navigationBarStyle)]) {
        navigationBar.barStyle = [self qmui_navigationBarStyle];
    }
    if ([self respondsToSelector:@selector(qmui_navigationBarShadowImage)]) {
        navigationBar.shadowImage = [self qmui_navigationBarShadowImage];
    }
    if ([self respondsToSelector:@selector(qmui_navigationBarTintColor)]) {
        navigationBar.tintColor = [self qmui_navigationBarTintColor];
    }
    if ([self respondsToSelector:@selector(qmui_titleViewTintColor)]) {
        self.titleView.tintColor = [self qmui_titleViewTintColor];
    }
}
 
#pragma mark - <QMUINavigationControllerDelegate>
 
- (BOOL)preferredNavigationBarHidden {
    return NavigationBarHiddenInitially;
}
 
- (void)viewControllerKeepingAppearWhenSetViewControllersWithAnimated:(BOOL)animated {
    // 通常和 viewWillAppear: 里做的事情保持一致
    [self setupNavigationItems];
    [self setupToolbarItems];
}
 
@end
 
@implementation QMUICommonViewController (QMUIKeyboard)
 
- (UITapGestureRecognizer *)hideKeyboardTapGestureRecognizer {
    return _hideKeyboardTapGestureRecognizer;
}
 
- (QMUIKeyboardManager *)hideKeyboardManager {
    return _hideKeyboardManager;
}
 
- (BOOL)shouldHideKeyboardWhenTouchInView:(UIView *)view {
    // 子类重写,默认返回 NO,也即不主动干预键盘的状态
    return NO;
}
 
@end
 
@implementation QMUIViewControllerHideKeyboardDelegateObject
 
- (instancetype)initWithViewController:(QMUICommonViewController *)viewController {
    if (self = [super init]) {
        self.viewController = viewController;
    }
    return self;
}
 
#pragma mark - <UIGestureRecognizerDelegate>
 
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer != self.viewController.hideKeyboardTapGestureRecognizer) {
        return YES;
    }
    
    if (![QMUIKeyboardManager isKeyboardVisible]) {
        return NO;
    }
    
    UIView *targetView = gestureRecognizer.qmui_targetView;
    
    // 点击了本身就是输入框的 view,就不要降下键盘了
    if ([targetView isKindOfClass:[UITextField class]] || [targetView isKindOfClass:[UITextView class]]) {
        return NO;
    }
    
    if ([self.viewController shouldHideKeyboardWhenTouchInView:targetView]) {
        [self.viewController.view endEditing:YES];
    }
    return NO;
}
 
#pragma mark - <QMUIKeyboardManagerDelegate>
 
- (void)keyboardWillShowWithUserInfo:(QMUIKeyboardUserInfo *)keyboardUserInfo {
    if (![self.viewController qmui_isViewLoadedAndVisible]) return;
    self.viewController.hideKeyboardTapGestureRecognizer.enabled = YES;
}
 
- (void)keyboardWillHideWithUserInfo:(QMUIKeyboardUserInfo *)keyboardUserInfo {
    self.viewController.hideKeyboardTapGestureRecognizer.enabled = NO;
}
 
@end