Repository navigation
Expand file tree
/
Copy pathUIAlertController+AsAlertView.m
More file actions
165 lines (136 loc) · 4.99 KB
/
Copy pathUIAlertController+AsAlertView.m
File metadata and controls
165 lines (136 loc) · 4.99 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
//
// UIAlertController+AsAlertView.m
//
// Created by DimasSup on 03.02.16.
//
#import "UIAlertController+AsAlertView.h"
#import <objc/runtime.h>
@interface UIAlertController (iOS9TintFix)
+ (void)tintFix;
- (void)swizzledViewWillAppear:(BOOL)animated;
@end
@implementation UIAlertController (iOS9TintFix)
+ (void)tintFix {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method method = class_getInstanceMethod(self, @selector(viewWillAppear:));
Method swizzle = class_getInstanceMethod(self, @selector(swizzledViewWillAppear:));
method_exchangeImplementations(method, swizzle);
method = class_getInstanceMethod(self, @selector(viewWillTransitionToSize:withTransitionCoordinator:));
swizzle = class_getInstanceMethod(self, @selector(swizzledViewWillTransitionToSize:withTransitionCoordinator:));
method_exchangeImplementations(method, swizzle);
});
}
- (void)swizzledViewWillAppear:(BOOL)animated {
[self swizzledViewWillAppear:animated];
for (UIView *view in self.view.subviews) {
if (view.tintColor == self.view.tintColor) {
//only do those that match the main view, so we don't strip the red-tint from destructive buttons.
self.view.tintColor = [UIColor colorWithRed:0.000 green:0.478 blue:1.000 alpha:1.000];
[view setNeedsDisplay];
}
}
}
-(void)swizzledViewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[self swizzledViewWillTransitionToSize:size withTransitionCoordinator:coordinator];
for (UIView *view in self.view.subviews) {
if (view.tintColor == self.view.tintColor) {
//only do those that match the main view, so we don't strip the red-tint from destructive buttons.
self.view.tintColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
[view setNeedsDisplay];
}
}
}
@end
@implementation UIAlertController (AsAlertView)
+(void)initialize
{
[super initialize];
[self tintFix];
}
+(nonnull UIAlertController*)initWithTitle:(NSString *)title message:(NSString *)message delegate:(__weak id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString*)otherButtonTitles,...
{
UIAlertController* controller = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
NSMutableArray *tmplist = [NSMutableArray arrayWithObject:otherButtonTitles];
if(otherButtonTitles!=nil)
{
va_list args;
va_start(args,otherButtonTitles);
id arg;
while((arg = va_arg(args, id)) != nil)
[tmplist addObject:arg];
va_end(args);
}
int c = 0;
if(tmplist.count<=1)
{
[controller addAction:[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
int c2 = c;
if([delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]){
[delegate alertView:controller clickedButtonAtIndex:c2];
}
if([delegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)]){
[delegate alertView:controller willDismissWithButtonIndex:c2];
}
if([delegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)]){
[delegate alertView:controller didDismissWithButtonIndex:c2];
}
}]];
c++;
}
for (NSString* arg in tmplist)
{
[controller addAction:[UIAlertAction actionWithTitle:arg style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
int c2 = c;
if([delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]){
[delegate alertView:controller clickedButtonAtIndex:c2];
}
if([delegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)]){
[delegate alertView:controller willDismissWithButtonIndex:c2];
}
if([delegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)]){
[delegate alertView:controller didDismissWithButtonIndex:c2];
}
}]];
c++;
}
if(tmplist.count>1)
{
[controller addAction:[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
int c2 = c;
if([delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]){
[delegate alertView:controller clickedButtonAtIndex:c2];
}
if([delegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)]){
[delegate alertView:controller willDismissWithButtonIndex:c2];
}
if([delegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)]){
[delegate alertView:controller didDismissWithButtonIndex:c2];
}
}]];
c++;
}
return controller;
}
-(void)show
{
UIViewController* vc = [[UIApplication sharedApplication].keyWindow rootViewController];
if(vc)
{
[vc presentViewController:self animated:YES completion:^{
}];
}
}
-(NSInteger)cancelButtonIndex
{
return [self.actions indexOfObjectPassingTest:^BOOL(UIAlertAction * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if(obj.style == UIAlertActionStyleCancel)
{
*stop = YES;
return YES;
}
return NO;
}];
}
@end