Skip to content

Commit

Permalink
[Tabs] fix TabBarViewControllerExample to respect safe area (#4949)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuzhang666 committed Aug 30, 2018
1 parent 837683b commit 4b00a6c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 46 deletions.
46 changes: 1 addition & 45 deletions components/Tabs/examples/TabBarViewControllerExample.m
Expand Up @@ -16,8 +16,6 @@

#import <UIKit/UIKit.h>

#import "MaterialButtons.h"
#import "MaterialButtons+ButtonThemer.h"
#import "MaterialColorScheme.h"
#import "MaterialSlider.h"
#import "MaterialTabs.h"
Expand All @@ -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];
}

Expand Down
Expand Up @@ -20,10 +20,13 @@

#import <UIKit/UIKit.h>

#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;
Expand All @@ -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<MDCButtonScheming>)buttonScheme
title:(nonnull NSString *)title
actionBlock:(nullable MDCButtonActionBlock)actionBlock;
@end
Expand Up @@ -23,6 +23,7 @@
#import "MaterialAppBar.h"
#import "MaterialAppBar+ColorThemer.h"
#import "MaterialAppBar+TypographyThemer.h"
#import "MaterialButtons.h"
#import "MaterialPalettes.h"

@interface TBVCSampleView : UIView
Expand All @@ -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
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -106,6 +120,28 @@ + (nonnull instancetype)sampleWithTitle:(nonnull NSString *)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)
Expand All @@ -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;
Expand Down

0 comments on commit 4b00a6c

Please sign in to comment.