forked from iamlay/Encryptions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseViewController.m
More file actions
442 lines (390 loc) · 14.1 KB
/
BaseViewController.m
File metadata and controls
442 lines (390 loc) · 14.1 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
// BaseViewController.m
// Encryption
//
// Created by 雷传营 on 16/1/10.
// Copyright © 2016年 leichuanying. All rights reserved.
//
#import "BaseViewController.h"
#import "MBProgressHUD.h"
#import "LSHelper.h"
#import "UIView+LSAdditions.h"
@interface BaseViewController () {
MBProgressHUD *progressView;
UISwipeGestureRecognizer *swipeLeftGR;
UILabel *_noDataTitle;
}
@end
@implementation BaseViewController
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_w = self.view.width;
_h = self.view.height;
self.view.backgroundColor = kColorViewControllerBg;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = NO;
self.automaticallyAdjustsScrollViewInsets = NO;
}
progressView = [[MBProgressHUD alloc] initWithView:self.view];
progressView.dimBackground = NO;
[self.view addSubview:progressView];
swipeLeftGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGR:)];
[self.view addGestureRecognizer:swipeLeftGR];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
swipeLeftGR.enabled = NO;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
/*
//代理置空,否则会闪退
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
*/
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
/*
//开启iOS7的滑动返回效果
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//只有在二级页面生效
if ([self.navigationController.viewControllers count] >= 2) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}else{
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
*/
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
/*
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
*/
}
- (BOOL)prefersStatusBarHidden
{
return NO;
}
#pragma mark - UIGestureRecongnizerDelegate
- (void)handleSwipeGR:(UIGestureRecognizer*)gestureRecognizer
{
if (UISwipeGestureRecognizerDirectionLeft)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
/**
* @brief 对当前的vc增加是否允许右滑的开关
* iOS7以下的系统interactivePopGestureRecognizer属性不存在
* @param
* flag:右滑手势
* popFlag:系统提供的右滑
*/
- (void)switchOfGR:(BOOL)flag
{
if (!flag)
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
else
{
swipeLeftGR.enabled = NO;
}
}
else
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
else
{
swipeLeftGR.enabled= YES;
}
}
}
/**
* @brief 自定义titlte居中处理
*
* @param text title
*/
- (void)setNavTitle:(NSString *)title
{
//注意必须先定义 leftBarButtonItem和rightBarButtonItem的位置
//注意,此处不使用self.navigationItem.rightBarButtonItems这样形式的用法
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainBoundsWidth, 44)];
titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
titleView.autoresizesSubviews = YES;
titleView.backgroundColor = [UIColor clearColor];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kMainBoundsWidth, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];;
titleLabel.font = [UIFont boldSystemFontOfSize:18];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// titleLabel.lineBreakMode = kLineBreakModeClip;
titleLabel.autoresizingMask = titleView.autoresizingMask;
//标题宽度
CGFloat width = [LSHelper widthForLabelWithString:title withFontSize:18 withWidth:kMainBoundsWidth withHeight:44];
CGFloat maxWidth = 120;
if(width <= kMainBoundsWidth-2*maxWidth){
titleLabel.frame = CGRectMake(0, 0, kMainBoundsWidth-maxWidth*2, 44);
titleView.frame = CGRectMake(maxWidth, 0, kMainBoundsWidth-maxWidth*2, 44);
}
else{
CGRect leftViewbounds = self.navigationItem.leftBarButtonItem.customView.bounds;
CGRect rightViewbounds = self.navigationItem.rightBarButtonItem.customView.bounds;
CGRect frame;
CGFloat maxWidth = leftViewbounds.size.width > rightViewbounds.size.width ? leftViewbounds.size.width : rightViewbounds.size.width;
maxWidth += 15;//leftview 左右都有间隙,左边是5像素,右边是8像素,加2个像素的阀值 5 + 8 + 2
frame = titleLabel.frame;
frame.size.width = kMainBoundsWidth - maxWidth * 2;
// frame = CGRectMake(maxWidth, 0, kMainBoundsWidth-maxWidth*2, 44);
titleLabel.frame = frame;
frame = titleView.frame;
frame.size.width = kMainBoundsWidth - maxWidth * 2;
// frame = CGRectMake(maxWidth, 0, kMainBoundsWidth-maxWidth*2, 44);
titleView.frame = frame;
}
titleLabel.text = title;
[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
}
- (NSAttributedString *)converHtml:(NSString *)htmlStr{
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
return attributedString;
}
- (void)setNavRightButtonwithImg:(NSString *)normalImg selImg:(NSString *)selImg title:(NSString *)title action:(SEL)action
{
self.navigationItem.rightBarButtonItem = [self getButtonItemWithImg:normalImg selImg:selImg title:title action:action];
}
- (void)setNavLeftButtonwithImg:(NSString *)normalImg selImg:(NSString *)selImg title:(NSString *)title action:(SEL)action
{
self.navigationItem.leftBarButtonItem = [self getButtonItemWithImg:normalImg selImg:selImg title:title action:action];
}
// 自定义的返回箭头
- (void)setNavBackArrow {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.exclusiveTouch = YES;
[button setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 50, 40);
if (kIsIOS7) {
[button setImageEdgeInsets:UIEdgeInsetsMake(0, -35, 0, 0)];
}else{
[button setImageEdgeInsets:UIEdgeInsetsMake(0, -25, 0, 0)];
}
[button addTarget:self action:@selector(navBackButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = backItem;
}
- (void)setNavRightButtonEnable:(BOOL)enable
{
[self.navigationItem.rightBarButtonItem setEnabled:enable];
}
- (void)setNavLeftButtonEnable:(BOOL)enable
{
[self.navigationItem.leftBarButtonItem setEnabled:enable];
}
#pragma mark - NavButton Clicked
- (void)navGoHomeButtonClicked:(UIButton *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)navBackButtonClicked:(UIButton *)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - MG_NavBaseViewController private
- (UIBarButtonItem *)getButtonItemWithImg:(NSString *)norImg selImg:(NSString *)selImg title:(NSString *)title action:(SEL)action
{
CGSize navbarSize = self.navigationController.navigationBar.bounds.size;
CGRect frame = CGRectMake(0, 0, navbarSize .height, navbarSize.height - 3);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.exclusiveTouch = YES;
if (norImg)
[button setImage:[UIImage imageNamed:norImg] forState:UIControlStateNormal];
if (selImg)
[button setImage:[UIImage imageNamed:selImg] forState:UIControlStateHighlighted];
if (title) {
CGSize strSize = CGSizeZero;
if (kIsIOS7) {
strSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]}];
} else {
strSize = [title sizeWithFont:[UIFont boldSystemFontOfSize:14]];
}
[button setTitle:title forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:RGBA(198, 198, 198, 1) forState:UIControlStateHighlighted];
frame.size.width = MAX(frame.size.width, strSize.width + 20);
}
button.frame = frame;
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* tmpBarBtnItem=[[UIBarButtonItem alloc] initWithCustomView:button];
return tmpBarBtnItem;
}
#pragma mark - ProgressView Event
- (void)showProgressViewWithTitle:(NSString *)title
{
[self.view bringSubviewToFront:progressView];
if (title)
progressView.labelText = title;
[progressView show:YES];
}
- (void)hideProgressView:(BOOL)animation
{
[progressView hide:animation];
}
- (void)hideProgressView
{
[progressView hide:YES];
}
#pragma mark 无数据展示界面
- (void)showNoDataView:(NSString *)title {
if (!_noDataTitle) {
_noDataTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.view.width, 18)];
_noDataTitle.textAlignment = NSTextAlignmentCenter;
_noDataTitle.textColor = kColorGrayText;
_noDataTitle.text = title;
_noDataTitle.font = [UIFont systemFontOfSize:15];
[self.view addSubview:_noDataTitle];
}
}
- (void)hideNoDataView {
[_noDataTitle removeFromSuperview];
}
//
//
//-(void)showNoNetWorkView
//{
// if (!_noNetWorkView) {
// _noNetWorkView = [LSNoNetWorkView loadFromXib];
// _noNetWorkView.delegate = self;
// }
// if (![LSReachablity shareInstance].getNetWorkStatus) {
// [_noNetWorkView showInView:self.view style:LSNoNetWorkViewStyle_No_NetWork];
// }else{
// [_noNetWorkView showInView:self.view style:LSNoNetWorkViewStyle_Load_Fail];
// }
//}
//-(void)showNoNetWorkView:(LSNoNetWorkViewStyle)style
//{
// if (!_noNetWorkView) {
// _noNetWorkView = [LSNoNetWorkView loadFromXib];
// _noNetWorkView.delegate = self;
// }
//}
//-(void)hideNoNetWorkView
//{
// [_noNetWorkView hide];
//}
//Overide method
#pragma mark LSNoNetWorkViewDelegate
-(void)retryToGetData
{
}
// 判断网络
- (BOOL)isNetOK{
NetworkStatus status = [[Reachability reachabilityWithHostName:@"www.baidu.com"] currentReachabilityStatus];
if (status == NotReachable) {
CLog(@"Connected Failed!!");
return NO;
}else{
CLog(@"Connected Successfully!!");
return YES;
}
// NetworkStatus status = [[Reachability reachabilityWithHostName:@"www.baidu.com"] currentReachabilityStatus];
// NetworkStatus statusJavaSever = [[Reachability reachabilityWithHostName:@"www.baidu.com"] currentReachabilityStatus];
// NetworkStatus statusPHPSever = [[Reachability reachabilityWithHostName:@"www.baidu.com"] currentReachabilityStatus];
// if (status == NotReachable ) {
// CLog(@"Connected Failed!!");
//
// return NO;
// }else{
// CLog(@"Connected Successfully!!");
// if (statusJavaSever == NotReachable && statusPHPSever == NotReachable) {
// CLog(@"服务器崩溃");
// return NO;
// }
// else
// {
// return YES;
// }
// return NO;
// }
}
//-(void)showGuideView:(LSGuideType)type
//{
// UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kMainBoundsWidth, kMainBoundsHeight)];
// NSString* imageName = nil;
// switch (type) {
// case LSGuideType_Arround:
// imageName = @"bg_guide_arround";
// break;
// case LSGuideType_Account:
// imageName = @"bg_guide_account";
// break;
// default:
// break;
// }
// if (is4Inch) {
// imageName = [NSString stringWithFormat:@"%@4.png",imageName];
// }else{
// imageName = [NSString stringWithFormat:@"%@3.png",imageName];
// }
// imageView.tag = TAG_GUIDE_VIEW;
// imageView.image = IMAGE_AT_APPDIR(imageName);
// imageView.userInteractionEnabled = YES;
// [[UIApplication sharedApplication].keyWindow addSubview:imageView];
// UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideGuideView)];
// [imageView addGestureRecognizer:tapGesture];
//}
//
//-(void)hideGuideView
//{
// [[[UIApplication sharedApplication].keyWindow viewWithTag:TAG_GUIDE_VIEW]removeFromSuperview];
//}
- (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;
}
@end