-
Notifications
You must be signed in to change notification settings - Fork 944
/
TabBarViewControllerExampleSupplemental.m
214 lines (175 loc) · 7.61 KB
/
TabBarViewControllerExampleSupplemental.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
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
/*
Copyright 2017-present the Material Components for iOS authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* IMPORTANT:
This file contains supplemental code used to populate the examples with dummy data and/or
instructions. It is not necessary to import this file to use Material Components for iOS.
*/
#import "TabBarViewControllerExampleSupplemental.h"
#import "MaterialAppBar.h"
#import "MaterialAppBar+ColorThemer.h"
#import "MaterialAppBar+TypographyThemer.h"
#import "MaterialButtons.h"
#import "MaterialPalettes.h"
@interface TBVCSampleView : UIView
@end
@implementation TBVCSampleView
// Draw a frame inset around our content area, to show that view are being resized correctly.
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
// In this example, it happens to be the appBarViewController.headerView
UIView *header = self.subviews.firstObject;
CGRect bounds = self.bounds;
CGRect dontCare;
CGRectDivide(bounds, &dontCare, &bounds, header.bounds.size.height, CGRectMinYEdge);
bounds = CGRectInset(bounds, 4, 4);
[UIColor.whiteColor set];
UIRectFrame(bounds);
}
@end
@interface TBVCSampleViewController ()
@property(nonatomic) MDCAppBarViewController *appBarViewController;
@property(nonatomic) UILabel *titleLabel;
@property(nonatomic) CGRect buttonFrame; // The desired frame of the button
@property(nonatomic) MDCButton *button;
@property(nonatomic, copy) MDCButtonActionBlock buttonActionBlock;
@end
@implementation TBVCSampleViewController
- (void)loadView {
self.view = [[TBVCSampleView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.appBarViewController = [[MDCAppBarViewController alloc] init];
[self addChildViewController:self.appBarViewController];
[self.view addSubview:self.appBarViewController.view];
[self.appBarViewController didMoveToParentViewController:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[MDCAppBarColorThemer applyColorScheme:self.colorScheme
toAppBarViewController:self.appBarViewController];
[MDCAppBarTypographyThemer applyTypographyScheme:self.typographyScheme
toAppBarViewController:self.appBarViewController];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_titleLabel.center = self.view.center;
UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
if (@available(iOS 11.0, *)) {
safeAreaInsets = self.view.safeAreaInsets;
}
CGRect buttonFrame = self.buttonFrame;
self.button.frame = CGRectOffset(buttonFrame, safeAreaInsets.left, safeAreaInsets.top);
[self.button sizeToFit];
[self.view setNeedsDisplay];
}
- (UIViewController *)childViewControllerForStatusBarStyle {
return self.appBarViewController;
}
+ (nonnull instancetype)sampleWithTitle:(nonnull NSString *)title color:(nonnull UIColor *)color {
return [self sampleWithTitle:title color:color icon:nil];
}
+ (nonnull instancetype)sampleWithTitle:(nonnull NSString *)title
color:(nonnull UIColor *)color
icon:(nullable UIImage *)icon {
TBVCSampleViewController *sample = [[TBVCSampleViewController alloc] init];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = title;
titleLabel.textColor = UIColor.whiteColor;
[titleLabel sizeToFit];
TBVCSampleView *sampleView = (TBVCSampleView *)sample.view;
sample.titleLabel = titleLabel;
[sampleView addSubview:titleLabel];
sampleView.backgroundColor = color;
sample.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:icon tag:0];
sample.title = title;
return sample;
}
- (void)setMDCButtonWithFrame:(CGRect)frame
buttonScheme:(nonnull id<MDCButtonScheming>)buttonScheme
title:(nonnull NSString *)title
actionBlock:(nullable MDCButtonActionBlock)actionBlock {
MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectZero];
[button setTitle:title forState:UIControlStateNormal];
[MDCContainedButtonThemer applyScheme:buttonScheme toButton:button];
[self.view addSubview:button];
self.button = button;
self.buttonFrame = CGRectStandardize(frame);
self.buttonActionBlock = actionBlock;
[button addTarget:self
action:@selector(triggerButtonActionHandler)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)triggerButtonActionHandler {
if (self.buttonActionBlock) {
self.buttonActionBlock();
}
}
@end
@implementation TabBarViewControllerExample (Supplemental)
- (void)setupTabBarColors {
self.tabBar.unselectedItemTintColor = MDCPalette.greyPalette.tint900;
self.tabBar.selectedItemTintColor = MDCPalette.bluePalette.tint500;
}
- (nonnull NSArray *)constructExampleViewControllers {
NSBundle *bundle = [NSBundle bundleForClass:[TabBarViewControllerExample class]];
MDCButtonScheme *buttonScheme = [[MDCButtonScheme alloc] init];
buttonScheme.colorScheme = self.colorScheme;
buttonScheme.typographyScheme = self.typographyScheme;
TBVCSampleViewController *child1 =
[TBVCSampleViewController sampleWithTitle:@"One" color:UIColor.redColor];
UIColor *blue = [UIColor colorWithRed:0x3A / 255.f green:0x56 / 255.f blue:0xFF / 255.f alpha:1];
TBVCSampleViewController *child2 = [TBVCSampleViewController sampleWithTitle:@"Two" color:blue];
__weak TabBarViewControllerExample *weakSelf = self;
[child2 setMDCButtonWithFrame:CGRectMake(10, 120, 300, 40)
buttonScheme:buttonScheme
title:@"Push and Hide Tab"
actionBlock:^{
TabBarViewControllerExample *strongSelf = weakSelf;
TBVCSampleViewController *vc =
[TBVCSampleViewController sampleWithTitle:@"Push&Hide" color:UIColor.grayColor];
vc.colorScheme = strongSelf.colorScheme;
vc.typographyScheme = strongSelf.typographyScheme;
[strongSelf.navigationController pushViewController:vc animated:YES];
}];
UIImage *starImage =
[UIImage imageNamed:@"TabBarDemo_ic_star" inBundle:bundle compatibleWithTraitCollection:nil];
TBVCSampleViewController *child3 =
[TBVCSampleViewController sampleWithTitle:@"Three" color:UIColor.blueColor icon:starImage];
[child3 setMDCButtonWithFrame:CGRectMake(10, 120, 300, 40)
buttonScheme:buttonScheme
title:@"Toggle Tab Bar"
actionBlock:^{
TabBarViewControllerExample *strongSelf = weakSelf;
[strongSelf setTabBarHidden:!strongSelf.tabBarHidden animated:YES];
}];
NSArray *viewControllers = @[ child1, child2, child3 ];
for (TBVCSampleViewController *vc in viewControllers) {
vc.colorScheme = self.colorScheme;
vc.typographyScheme = self.typographyScheme;
}
return viewControllers;
}
@end
@implementation TabBarViewControllerExample (CatalogByConvention)
+ (NSDictionary *)catalogMetadata {
return @{
@"breadcrumbs": @[ @"Tab Bar", @"TabBarViewController" ],
@"primaryDemo": @NO,
@"presentable": @NO,
};
}
- (BOOL)catalogShouldHideNavigation {
return YES;
}
@end