material-components_materia.../components/AppBar/examples/AppBarDelegateForwardingExample.m
Junius Gunaratne 205b1b49fd [Catalog, NavigationBar] Update catalog to set custom title color using titleTextAttributes; only set attributedText when title is set
Reviewers: ajsecord, O1 Material components iOS

Reviewed By: ajsecord, O1 Material components iOS

Tags: #material_components_ios

Differential Revision: http://codereview.cc/D1480
2016-08-17 15:06:16 -04:00

149 lines
4.7 KiB
Objective-C

/*
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"
// This example builds upon AppBarTypicalUseExample.
@interface AppBarDelegateForwardingExample : UITableViewController
@property(nonatomic, strong) MDCAppBar *appBar;
@end
@implementation AppBarDelegateForwardingExample
- (void)viewDidLoad {
[super viewDidLoad];
// UITableViewController's tableView.delegate is self by default. We're setting it here for
// emphasis.
self.tableView.delegate = self;
self.appBar.headerViewController.headerView.trackingScrollView = self.tableView;
[self.appBar addSubviewsToParent];
self.tableView.layoutMargins = UIEdgeInsetsZero;
self.tableView.separatorInset = UIEdgeInsetsZero;
}
#pragma mark - UIScrollViewDelegate
// The following four methods must be forwarded to the tracking scroll view in order to implement
// the Flexible Header's behavior.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.appBar.headerViewController.headerView.trackingScrollView) {
[self.appBar.headerViewController.headerView trackingScrollViewDidScroll];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView == self.appBar.headerViewController.headerView.trackingScrollView) {
[self.appBar.headerViewController.headerView trackingScrollViewDidEndDecelerating];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
MDCFlexibleHeaderView *headerView = self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView trackingScrollViewDidEndDraggingWillDecelerate:decelerate];
}
}
- (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 AppBarDelegateForwardingExample (CatalogByConvention)
+ (NSArray *)catalogBreadcrumbs {
return @[ @"App Bar", @"Delegate Forwarding" ];
}
- (BOOL)catalogShouldHideNavigation {
return YES;
}
@end
@implementation AppBarDelegateForwardingExample (TypicalUse)
- (id)init {
self = [super init];
if (self) {
_appBar = [[MDCAppBar alloc] init];
_appBar.navigationBar.tintColor = [UIColor whiteColor];
_appBar.navigationBar.titleTextAttributes =
@{NSForegroundColorAttributeName : [UIColor whiteColor]};
[self addChildViewController:_appBar.headerViewController];
self.title = @"Delegate Forwarding";
UIColor *color = [UIColor colorWithRed:(CGFloat)0x03 / (CGFloat)255
green:(CGFloat)0xA9 / (CGFloat)255
blue:(CGFloat)0xF4 / (CGFloat)255
alpha:1];
_appBar.headerViewController.headerView.backgroundColor = color;
}
return self;
}
- (UIViewController *)childViewControllerForStatusBarHidden {
return self.appBar.headerViewController;
}
- (UIViewController *)childViewControllerForStatusBarStyle {
return self.appBar.headerViewController;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
@end
#pragma mark - Typical application code (not Material-specific)
@implementation AppBarDelegateForwardingExample (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