UIViewController+Swizzling.m
6.0 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
//
// UIViewController+Swizzling.m
// PDA
//
// Created by KiTee on 2018/9/7.
// Copyright © 2018年 Linno. All rights reserved.
//
#import "UIViewController+Swizzling.h"
#import "BaseNaviController.h"
#import <objc/runtime.h>
@implementation UIViewController (Swizzling)
+ (void)load
{
// 拦截viewWillAppear:事件
SEL origSel0 = @selector(viewWillAppear:);
SEL swizSel0 = @selector(swiz_viewWillAppear:);
[UIViewController swizzleMethods:[self class] originalSelector:origSel0 swizzledSelector:swizSel0];
// 拦截viewDidLoad事件
SEL origSel = @selector(viewDidLoad);
SEL swizSel = @selector(swiz_viewDidLoad:);
[UIViewController swizzleMethods:[self class] originalSelector:origSel swizzledSelector:swizSel];
// 拦截viewDidAppear:事件
SEL origSel2 = @selector(viewDidAppear:);
SEL swizSel2 = @selector(swiz_viewDidAppear:);
[UIViewController swizzleMethods:[self class] originalSelector:origSel2 swizzledSelector:swizSel2];
}
// exchange implementation of two methods
+ (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel
{
Method origMethod = class_getInstanceMethod(class, origSel);
Method swizMethod = class_getInstanceMethod(class, swizSel);
// class_addMethod will fail if original method already exists
BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
if (didAddMethod) {
class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
// origMethod and swizMethod already exist
method_exchangeImplementations(origMethod, swizMethod);
}
}
#pragma mark - 响应自定义viewWillAppear:事件
- (void)swiz_viewWillAppear:(BOOL)animated {
#pragma mark - 处理导航栏显示/隐藏
self.navigationController.navigationBarHidden = ([self respondsToSelector:@selector(isShowNavigationBar)] && [self isShowNavigationBar] == YES) ? YES : NO;
#pragma mark - 根据不同页面设置不同的导航栏风格
if ([self respondsToSelector:@selector(navigationBarStyle)]) {
[BaseNaviController setNavigationBarStyle:[self navigationBarStyle] vc:self];
}
// 系统拍照和相册页面调试
// NSString *className = NSStringFromClass([self class]);
// NSArray *classArrary = @[@"JWMImagePickerController",
// @"CAMImagePickerCameraViewController", @"CAMViewfinderViewController", @"CAMPreviewViewController",
// @"UIInputWindowController", @"UIEditingOverlayViewController", @"PUPhotoPickerHostViewController",
// @"PLPhotoTileViewController"];
#pragma mark - 处理用户打开自动切换主题按钮,根据设定好的时间段进行主题切换
if (kGetUserDefaultsBOOL(ThemeAutoSwitch)) {
if ([NSDate judgeTimeWithStartTime:StartTime1 expireTime:ExpireTime1] || [NSDate judgeTimeWithStartTime:StartTime2 expireTime:ExpireTime2]) {
self.dk_manager.themeVersion = DKThemeVersionNight;
} else {
self.dk_manager.themeVersion = DKThemeVersionNormal;
}
}
#pragma mark - 重返系统事件
[self swiz_viewWillAppear:animated];
}
#pragma mark - 响应自定义viewDidLoad事件
- (void)swiz_viewDidLoad:(BOOL)animated
{
#pragma mark - 处理返回按钮显示/隐藏
self.navigationItem.hidesBackButton = ([self respondsToSelector:@selector(isHideBackButton)] && [self isHideBackButton] == YES) ? YES : NO;
#pragma mark - 处理返回按钮标题显示/隐藏
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
#pragma mark - 重返系统事件
[self swiz_viewDidLoad:animated];
}
#pragma mark - 响应自定义viewDidAppear:事件
- (void)swiz_viewDidAppear:(BOOL)animated
{
#pragma mark - 处理侧滑
// 默认打开侧滑
BOOL enabled = YES;
if (self.navigationController.viewControllers.count == 1) {
// 只有一个控制器的时候禁止侧滑,防止页面卡死
enabled = NO;
} else {
if ([self respondsToSelector:@selector(enableInteractivePopGestureRecognizer)] && [self enableInteractivePopGestureRecognizer] == NO) {
enabled = NO;
}
}
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = enabled;
}
#pragma mark - 页面浏览统计
#pragma mark - 重返系统事件
[self swiz_viewDidAppear:animated];
}
#pragma mark - 暗黑模式跟随系统(通过Hook去实现)
/*
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
if (@available(iOS 13.0, *)) {
// 应用主题设置添加是否跟随系统按钮,主题模式跟随系统设置
if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
// 执行操作
NSString *currentTheme = self.dk_manager.themeVersion;
if ([currentTheme isEqualToString:DKThemeVersionNight]) {
currentTheme = DKThemeVersionNormal;
} else if ([currentTheme isEqualToString:DKThemeVersionNormal]) {
currentTheme = DKThemeVersionNight;
}
self.dk_manager.themeVersion = currentTheme;
[BaseNaviController setNavigationBarStyle:[self navigationBarStyle] vc:self];
[self.navigationController preferredStatusBarStyle];
[self setNeedsStatusBarAppearanceUpdate];
}
} else {
// 低版本的不显示跟随系统按钮,主题模式根据用户操作来设置
}
}
*/
@end