-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
UIViewController+RNNOptions.m
174 lines (150 loc) · 6.12 KB
/
UIViewController+RNNOptions.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
169
170
171
172
173
174
#import "RNNBackButtonOptions.h"
#import "RNNBottomTabOptions.h"
#import "RNNNavigationOptions.h"
#import "UIViewController+RNNOptions.h"
#import <React/RCTRootView.h>
#define kStatusBarAnimationDuration 0.35
const NSInteger BLUR_STATUS_TAG = 78264801;
@implementation UIViewController (RNNOptions)
- (void)setBackgroundImage:(UIImage *)backgroundImage {
if (backgroundImage) {
UIImageView *backgroundImageView = self.view.subviews.firstObject;
if (![backgroundImageView isKindOfClass:[UIImageView class]]) {
backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:backgroundImageView atIndex:0];
}
backgroundImageView.layer.masksToBounds = YES;
backgroundImageView.image = backgroundImage;
[backgroundImageView setContentMode:UIViewContentModeScaleAspectFill];
}
}
- (void)setSearchBarWithOptions:(NSString *)placeholder
focus:(BOOL)focus
hideTopBarOnFocus:(BOOL)hideTopBarOnFocus
hideOnScroll:(BOOL)hideOnScroll
obscuresBackgroundDuringPresentation:(BOOL)obscuresBackgroundDuringPresentation
backgroundColor:(nullable UIColor *)backgroundColor
tintColor:(nullable UIColor *)tintColor
cancelText:(NSString *)cancelText {
if (!self.navigationItem.searchController) {
UISearchController *search =
[[UISearchController alloc] initWithSearchResultsController:nil];
search.dimsBackgroundDuringPresentation = obscuresBackgroundDuringPresentation;
if ([self conformsToProtocol:@protocol(UISearchResultsUpdating)]) {
[search setSearchResultsUpdater:((UIViewController<UISearchResultsUpdating> *)self)];
}
search.searchBar.delegate = (id<UISearchBarDelegate>)self;
if (placeholder) {
search.searchBar.placeholder = placeholder;
}
if (cancelText) {
[search.searchBar setValue:cancelText forKey:@"cancelButtonText"];
}
search.hidesNavigationBarDuringPresentation = hideTopBarOnFocus;
search.searchBar.searchBarStyle = UISearchBarStyleProminent;
search.searchBar.tintColor = tintColor;
if (@available(iOS 13.0, *)) {
search.searchBar.searchTextField.backgroundColor = backgroundColor;
}
if (focus) {
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationItem.searchController.active = true;
[self.navigationItem.searchController.searchBar becomeFirstResponder];
});
}
self.navigationItem.searchController = search;
[self.navigationItem setHidesSearchBarWhenScrolling:hideOnScroll];
// Fixes #3450, otherwise, UIKit will infer the presentation context to
// be the root most view controller
self.definesPresentationContext = YES;
}
}
- (void)setSearchBarHiddenWhenScrolling:(BOOL)searchBarHidden {
self.navigationItem.hidesSearchBarWhenScrolling = searchBarHidden;
}
- (void)setSearchBarVisible:(BOOL)visible {
if (!visible) {
self.navigationItem.searchController = nil;
}
}
- (void)setNavigationItemTitle:(NSString *)title {
self.navigationItem.title = title;
}
- (void)setTabBarItemBadge:(NSString *)badge {
UITabBarItem *tabBarItem = self.tabBarItem;
if ([badge isKindOfClass:[NSNull class]] || [badge isEqualToString:@""]) {
tabBarItem.badgeValue = nil;
} else {
tabBarItem.badgeValue = badge;
[[self.tabBarController.tabBar viewWithTag:tabBarItem.tag] removeFromSuperview];
tabBarItem.tag = -1;
}
}
- (void)setTabBarItemBadgeColor:(UIColor *)badgeColor {
if (@available(iOS 13.0, *)) {
self.tabBarItem.standardAppearance.stackedLayoutAppearance.normal.badgeBackgroundColor =
badgeColor;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000
if (@available(iOS 15.0, *)) {
self.tabBarItem.scrollEdgeAppearance.stackedLayoutAppearance.normal
.badgeBackgroundColor = badgeColor;
}
#endif
} else {
self.tabBarItem.badgeColor = badgeColor;
}
}
- (void)setStatusBarStyle:(NSString *)style animated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:[self statusBarAnimationDuration:animated]
animations:^{
[self setNeedsStatusBarAppearanceUpdate];
}];
} else {
[self setNeedsStatusBarAppearanceUpdate];
}
}
- (void)setTopBarPrefersLargeTitle:(BOOL)prefersLargeTitle {
if (prefersLargeTitle) {
self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
} else {
self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
}
}
- (void)setStatusBarBlur:(BOOL)blur {
UIView *curBlurView = [self.view viewWithTag:BLUR_STATUS_TAG];
if (blur) {
if (!curBlurView) {
UIVisualEffectView *blur = [[UIVisualEffectView alloc]
initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular]];
blur.frame = [[UIApplication sharedApplication] statusBarFrame];
blur.tag = BLUR_STATUS_TAG;
[self.view addSubview:blur];
}
} else {
if (curBlurView) {
[curBlurView removeFromSuperview];
}
}
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
self.view.backgroundColor = backgroundColor;
}
- (void)setBackButtonVisible:(BOOL)visible {
self.navigationItem.hidesBackButton = !visible;
}
- (CGFloat)statusBarAnimationDuration:(BOOL)animated {
return animated ? kStatusBarAnimationDuration : CGFLOAT_MIN;
}
- (BOOL)isModal {
if ([self presentingViewController])
return YES;
if ([[[self navigationController] presentingViewController] presentedViewController] ==
[self navigationController])
return YES;
if ([[[self tabBarController] presentingViewController]
isKindOfClass:[UITabBarController class]])
return YES;
return NO;
}
@end