blob: b6de7ecf5ebb376fd7ad964e4e3d812a5d1e026c [file]
/*
Copyright 2016-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 <UIKit/UIKit.h>
#import "MaterialAppBar.h"
#import "MaterialAppBar+ColorThemer.h"
#import "MaterialAppBar+TypographyThemer.h"
@interface AppBarTypicalUseExample : UITableViewController
// Step 1: Create an App Bar.
@property(nonatomic, strong) MDCAppBar *appBar;
@property(nonatomic, strong) MDCSemanticColorScheme *colorScheme;
@property(nonatomic, strong) MDCTypographyScheme *typographyScheme;
@end
@implementation AppBarTypicalUseExample
- (id)init {
self = [super init];
if (self) {
self.title = @"App Bar";
// Step 2: Initialize the App Bar and add the headerViewController as a child.
_appBar = [[MDCAppBar alloc] init];
[self addChildViewController:_appBar.headerViewController];
_appBar.headerViewController.headerView.shiftBehavior = MDCFlexibleHeaderShiftBehaviorEnabled;
[_appBar.headerViewController.headerView hideViewWhenShifted:_appBar.headerStackView];
_appBar.navigationBar.inkColor = [UIColor colorWithWhite:0.9f alpha:0.1f];
self.colorScheme = [[MDCSemanticColorScheme alloc] init];
self.typographyScheme = [[MDCTypographyScheme alloc] init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[MDCAppBarColorThemer applySemanticColorScheme:self.colorScheme toAppBar:_appBar];
[MDCAppBarTypographyThemer applyTypographyScheme:self.typographyScheme toAppBar:_appBar];
// Need to update the status bar style after applying the theme.
[self setNeedsStatusBarAppearanceUpdate];
// Recommended step: Set the tracking scroll view.
self.appBar.headerViewController.headerView.trackingScrollView = self.tableView;
// Step 3: Register the App Bar views.
[self.appBar addSubviewsToParent];
self.tableView.layoutMargins = UIEdgeInsetsZero;
self.tableView.separatorInset = UIEdgeInsetsZero;
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Right"
style:UIBarButtonItemStyleDone
target:nil
action:nil];
}
// Optional step: If you allow the header view to hide the status bar you must implement this
// method and return the headerViewController.
- (UIViewController *)childViewControllerForStatusBarHidden {
return self.appBar.headerViewController;
}
// Optional step: The Header View Controller does basic inspection of the header view's background
// color to identify whether the status bar should be light or dark-themed.
- (UIViewController *)childViewControllerForStatusBarStyle {
return self.appBar.headerViewController;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
MDCFlexibleHeaderView *headerView = self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView trackingScrollViewDidScroll];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
MDCFlexibleHeaderView *headerView = self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView trackingScrollViewDidEndDraggingWillDecelerate:decelerate];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
MDCFlexibleHeaderView *headerView = self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView trackingScrollViewDidEndDecelerating];
}
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset {
MDCFlexibleHeaderView *headerView = self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView trackingScrollViewWillEndDraggingWithVelocity:velocity
targetContentOffset:targetContentOffset];
}
}
@end
@implementation AppBarTypicalUseExample (CatalogByConvention)
+ (NSArray *)catalogBreadcrumbs {
return @[ @"App Bar", @"App Bar" ];
}
+ (NSString *)catalogDescription {
return @"The top app bar displays information and actions relating to the current view.";
}
+ (BOOL)catalogIsPrimaryDemo {
return YES;
}
- (BOOL)catalogShouldHideNavigation {
return YES;
}
+ (BOOL)catalogIsPresentable {
return YES;
}
@end
#pragma mark - Typical application code (not Material-specific)
@implementation AppBarTypicalUseExample (UITableViewDataSource)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.layoutMargins = UIEdgeInsetsZero;
return cell;
}
@end