diff --git a/components/Tabs/examples/TabBarViewControllerExample.m b/components/Tabs/examples/TabBarViewControllerExample.m index 149f16279b1..a35cc7397e9 100644 --- a/components/Tabs/examples/TabBarViewControllerExample.m +++ b/components/Tabs/examples/TabBarViewControllerExample.m @@ -16,8 +16,6 @@ #import -#import "MaterialButtons.h" -#import "MaterialButtons+ButtonThemer.h" #import "MaterialColorScheme.h" #import "MaterialSlider.h" #import "MaterialTabs.h" @@ -42,54 +40,12 @@ - (void)viewDidLoad { [self loadTabBar]; } -#pragma mark - Action - -- (void)toggleTabBar { - [self setTabBarHidden:!self.tabBarHidden animated:YES]; -} - -- (void)pushHidesNavigation { - TBVCSampleViewController *vc = - [TBVCSampleViewController sampleWithTitle:@"Push&Hide" color:UIColor.grayColor]; - vc.colorScheme = self.colorScheme; - vc.typographyScheme = self.typographyScheme; - [self.navigationController pushViewController:vc animated:YES]; -} - #pragma mark - Private - (void)loadTabBar { NSArray *viewControllers = [self constructExampleViewControllers]; self.viewControllers = viewControllers; - UIViewController *child0 = viewControllers[0]; - self.selectedViewController = child0; - UIViewController *child1 = viewControllers[1]; - - MDCButtonScheme *buttonScheme = [[MDCButtonScheme alloc] init]; - buttonScheme.colorScheme = self.colorScheme; - buttonScheme.typographyScheme = self.typographyScheme; - - // Put the button under the header. - MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectMake(10, 120, 300, 40)]; - [MDCContainedButtonThemer applyScheme:buttonScheme toButton:button]; - [button setTitle:@"Push and Hide Tab" forState:UIControlStateNormal]; - [button sizeToFit]; - [child1.view addSubview:button]; - [button addTarget:self - action:@selector(pushHidesNavigation) - forControlEvents:UIControlEventTouchUpInside]; - - UIViewController *child2 = viewControllers[2]; - // Put the button under the header. - button = [[MDCButton alloc] initWithFrame:CGRectMake(10, 120, 300, 40)]; - [MDCContainedButtonThemer applyScheme:buttonScheme toButton:button]; - [button setTitle:@"Toggle Tab Bar" forState:UIControlStateNormal]; - [button sizeToFit]; - [child2.view addSubview:button]; - [button addTarget:self - action:@selector(toggleTabBar) - forControlEvents:UIControlEventTouchUpInside]; - + self.selectedViewController = self.viewControllers.firstObject; [MDCTabBarColorThemer applySemanticColorScheme:self.colorScheme toTabs:self.tabBar]; } diff --git a/components/Tabs/examples/supplemental/TabBarViewControllerExampleSupplemental.h b/components/Tabs/examples/supplemental/TabBarViewControllerExampleSupplemental.h index c8922d5ce9d..b0b843f0828 100644 --- a/components/Tabs/examples/supplemental/TabBarViewControllerExampleSupplemental.h +++ b/components/Tabs/examples/supplemental/TabBarViewControllerExampleSupplemental.h @@ -20,10 +20,13 @@ #import +#import "MaterialButtons+ButtonThemer.h" #import "MaterialColorScheme.h" #import "MaterialTypographyScheme.h" #import "MaterialTabs.h" +typedef void (^MDCButtonActionBlock)(void); + @interface TabBarViewControllerExample : MDCTabBarViewController @property(nonatomic, strong, nullable) MDCSemanticColorScheme *colorScheme; @property(nonatomic, strong, nullable) MDCTypographyScheme *typographyScheme; @@ -38,7 +41,14 @@ @end @interface TBVCSampleViewController : UIViewController -+ (nonnull instancetype)sampleWithTitle:(nonnull NSString *)title color:(nonnull UIColor *)color; + @property(nonatomic, nullable) MDCSemanticColorScheme *colorScheme; @property(nonatomic, nullable) MDCTypographyScheme *typographyScheme; + ++ (nonnull instancetype)sampleWithTitle:(nonnull NSString *)title color:(nonnull UIColor *)color; + +- (void)setMDCButtonWithFrame:(CGRect)frame + buttonScheme:(nonnull id)buttonScheme + title:(nonnull NSString *)title + actionBlock:(nullable MDCButtonActionBlock)actionBlock; @end diff --git a/components/Tabs/examples/supplemental/TabBarViewControllerExampleSupplemental.m b/components/Tabs/examples/supplemental/TabBarViewControllerExampleSupplemental.m index 5e9f2883bfb..87f1dc24be9 100644 --- a/components/Tabs/examples/supplemental/TabBarViewControllerExampleSupplemental.m +++ b/components/Tabs/examples/supplemental/TabBarViewControllerExampleSupplemental.m @@ -23,6 +23,7 @@ #import "MaterialAppBar.h" #import "MaterialAppBar+ColorThemer.h" #import "MaterialAppBar+TypographyThemer.h" +#import "MaterialButtons.h" #import "MaterialPalettes.h" @interface TBVCSampleView : UIView @@ -46,8 +47,13 @@ - (void)drawRect:(CGRect)rect { @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 @@ -78,6 +84,14 @@ - (void)viewWillAppear:(BOOL)animated { - (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]; } @@ -106,6 +120,28 @@ + (nonnull instancetype)sampleWithTitle:(nonnull NSString *)title return sample; } +- (void)setMDCButtonWithFrame:(CGRect)frame + buttonScheme:(nonnull id)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) @@ -117,14 +153,40 @@ - (void)setupTabBarColors { - (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;