/* 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. */ #import #import "BottomNavigationTypicalUseSupplemental.h" #import "MaterialAppBar.h" #import "MaterialBottomNavigation.h" #import "MaterialPalettes.h" #import "MaterialBottomNavigation+ColorThemer.h" #import "MaterialBottomNavigation+TypographyThemer.h" @interface BottomNavigationTypicalUseExample () @property(nonatomic, assign) int badgeCount; @property(nonatomic, strong) MDCAppBar *appBar; @property(nonatomic, strong) MDCBottomNavigationBar *bottomNavBar; @end @implementation BottomNavigationTypicalUseExample - (id)init { self = [super init]; if (self) { self.title = @"Bottom Navigation"; [self commonBottomNavigationTypicalUseExampleInit]; } return self; } - (void)commonBottomNavigationTypicalUseExampleInit { [self setupAppBar]; _bottomNavBar = [[MDCBottomNavigationBar alloc] initWithFrame:CGRectZero]; _bottomNavBar.titleVisibility = MDCBottomNavigationBarTitleVisibilitySelected; _bottomNavBar.alignment = MDCBottomNavigationBarAlignmentJustifiedAdjacentTitles; _bottomNavBar.delegate = self; [self.view addSubview:_bottomNavBar]; UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@"Home"] tag:0]; UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"Messages" image:[UIImage imageNamed:@"Email"] tag:0]; UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTitle:@"Favorites" image:[UIImage imageNamed:@"Favorite"] tag:0]; UITabBarItem *tabBarItem4 = [[UITabBarItem alloc] initWithTitle:@"Search" image:[UIImage imageNamed:@"Search"] tag:0]; tabBarItem4.badgeValue = @"88"; UITabBarItem *tabBarItem5 = [[UITabBarItem alloc] initWithTitle:@"Birthday" image:[UIImage imageNamed:@"Cake"] tag:0]; tabBarItem5.badgeValue = @"999+"; #if defined(__IPHONE_10_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wpartial-availability" if ([tabBarItem5 respondsToSelector:@selector(badgeColor)]) { tabBarItem5.badgeColor = [MDCPalette cyanPalette].accent700; } #pragma clang diagnostic pop #endif _bottomNavBar.items = @[ tabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4, tabBarItem5 ]; _bottomNavBar.selectedItem = tabBarItem2; [self updateBadgeItemCount]; } - (void)layoutBottomNavBar { CGSize size = [_bottomNavBar sizeThatFits:self.view.bounds.size]; CGRect bottomNavBarFrame = CGRectMake(0, CGRectGetHeight(self.view.bounds) - size.height, size.width, size.height); _bottomNavBar.frame = bottomNavBarFrame; } - (void)viewDidLoad { [super viewDidLoad]; [self.appBar addSubviewsToParent]; [MDCBottomNavigationBarTypographyThemer applyTypographyScheme:self.typographyScheme toBottomNavigationBar:_bottomNavBar]; [MDCBottomNavigationBarColorThemer applySemanticColorScheme:self.colorScheme toBottomNavigation:_bottomNavBar]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; [self layoutBottomNavBar]; } #if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0) - (void)viewSafeAreaInsetsDidChange { if (@available(iOS 11.0, *)) { [super viewSafeAreaInsetsDidChange]; } [self layoutBottomNavBar]; } #endif - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:animated]; } - (void)updateBadgeItemCount { // Example of badge with increasing count. if (!self.badgeCount) { self.badgeCount = 0; } self.badgeCount++; self.bottomNavBar.items[1].badgeValue = [NSNumber numberWithInt:self.badgeCount].stringValue; __weak BottomNavigationTypicalUseExample *weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [weakSelf updateBadgeItemCount]; }); } #pragma mark - MDCBottomNavigationBarDelegate - (void)bottomNavigationBar:(nonnull MDCBottomNavigationBar *)bottomNavigationBar didSelectItem:(nonnull UITabBarItem *)item { NSLog(@"Selected Item: %@", item.title); } #pragma mark - Configure MDCAppBar for navigation - (UIViewController *)childViewControllerForStatusBarHidden { return self.appBar.headerViewController; } - (UIViewController *)childViewControllerForStatusBarStyle { return self.appBar.headerViewController; } - (void)setupAppBar { _appBar = [[MDCAppBar alloc] init]; [self addChildViewController:_appBar.headerViewController]; UIColor *color = [UIColor colorWithWhite:0.2f alpha:1]; _appBar.headerViewController.headerView.backgroundColor = color; _appBar.headerViewController.headerView.shiftBehavior = MDCFlexibleHeaderShiftBehaviorEnabled; [_appBar.headerViewController.headerView hideViewWhenShifted:_appBar.headerStackView]; _appBar.navigationBar.tintColor = [UIColor whiteColor]; _appBar.navigationBar.titleTextAttributes = @{ NSForegroundColorAttributeName : [UIColor whiteColor] }; self.view.backgroundColor = [UIColor lightGrayColor]; } @end