-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIViewController+UxUtils.m
168 lines (132 loc) · 5.97 KB
/
UIViewController+UxUtils.m
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
//
// UIViewController+ActivityInProgress.m
// Pending Screen + Alert iOS
//
// Created by Sergio Flores on 11/28/17.
// Copyright © 2017 SAFT.Industries. All rights reserved.
//
#import "UIViewController+UxUtils.h"
@implementation UIViewController (UxUtils)
#define TAG_INDICATOR_OVERLAY 1000
#define APP_TITLE @"Title"
#define LBL_OK @"Ok"
#define LBL_INPROGRESS @"In progress..."
/*
Shows a standard UIAlertController, with one default action, which currently does nothing, and returns to the calling UIViewController. If on iPad, it aligns the Alert to the calling View.
Params:
text - The message to show to the user.
sender - The view to align the alert with.
*/
- (void) showStandardMessageAlertWithText: (NSString*) text withSender:(id) sender
{
UIAlertController* alert = [UIAlertController
alertControllerWithTitle: APP_TITLE
message: text
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction
actionWithTitle:LBL_OK
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:defaultAction];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
alert.popoverPresentationController.sourceView = (UIView*) sender;
alert.popoverPresentationController.sourceRect = ((UIView*) sender).frame;
}
[self presentViewController:alert animated:YES completion:nil];
}
/*
Shows a standard UIAlertController, with one default action, which pops the calling UIViewController. Useful to automatically close a screen after giving some info to the user. If on iPad, it aligns the Alert to the calling View.
Params:
text - The message to show to the user.
sender - The view to align the alert with.
*/
- (void) showPopVCMessageAlertWithText: (NSString*) text withSender:(id) sender
{
UIAlertController* alert = [UIAlertController
alertControllerWithTitle: APP_TITLE
message: text
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction
actionWithTitle:LBL_OK
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self.navigationController popViewControllerAnimated:YES];
}];
[alert addAction:defaultAction];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
alert.popoverPresentationController.sourceView = (UIView*) sender;
alert.popoverPresentationController.sourceRect = ((UIView*) sender).frame;
}
[self presentViewController:alert animated:YES completion:nil];
}
- (void) showPendingScreen
{
UIView* areaView = [[UIApplication sharedApplication] keyWindow];
UIView* overlayView = [[UIView alloc] initWithFrame:
CGRectMake(areaView.frame.origin.x,
areaView.frame.origin.y,
areaView.frame.size.width,
areaView.frame.size.height)];
overlayView.center = areaView.center;
overlayView.alpha = 0;
overlayView.backgroundColor = [UIColor blackColor];
overlayView.tag = TAG_INDICATOR_OVERLAY;
[areaView addSubview:overlayView];
[areaView bringSubviewToFront:overlayView];
[UIViewPropertyAnimator
runningPropertyAnimatorWithDuration:0.5
delay:0
options: UIViewAnimationOptionCurveLinear
animations:
^{
overlayView.alpha = 0;
overlayView.alpha = 0.5;
}
completion:nil];
UIActivityIndicatorView* indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicatorView.center = overlayView.center;
[indicatorView startAnimating];
[overlayView addSubview:indicatorView];
UILabel *indicatorLabel = [[UILabel alloc] init];
[indicatorLabel setText: LBL_INPROGRESS];
[indicatorLabel setTextAlignment:NSTextAlignmentCenter];
[indicatorLabel setNumberOfLines:0];
[indicatorLabel setFont: [UIFont systemFontOfSize:15]];
[indicatorLabel setTextColor:[UIColor whiteColor]];
[indicatorLabel sizeToFit];
indicatorLabel.center = CGPointMake(indicatorView.center.x, indicatorView.center.y + 30);
[overlayView addSubview:indicatorLabel];
}
/*
Locates the overlay view, identified by the TAG and destroys it.
*/
- (void) hidePendingScreen
{
UIView* areaView = [[UIApplication sharedApplication] keyWindow];
for (UIView *currentView in areaView.subviews)
{
if (currentView.tag == TAG_INDICATOR_OVERLAY)
[currentView removeFromSuperview];
}
}
// Call it from a given element in use, like a textField to make the keyboard go away.
- (IBAction) dismisKBFromField:(id)sender
{
[sender resignFirstResponder];
}
// Call it from a ViewController to remove the keyboard, regardless of what screen element is in use.
-(void) dismisKBFromView:(id)sender
{
[self.view endEditing:YES];
}
// Call it from viewDidLoad to let the user dismiss the keyboard by tapping in any empty space on the screen.
- (void) removeKeyboardFromBackground
{
UITapGestureRecognizer* tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismisKBFromView:)];
[tapBackground setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapBackground];
}
@end