material-components_materia.../components/AppBar/examples/AppBarTypicalCollectionViewExample.m
Mohammad Cazi f7869ea61b
[AppBar] Added a AppBarTypicalCollectionViewExample. (#3491)
* Added a AppBarTypicalCollectionViewExample.

* applying scheme in view did load, adding scheming properties to the example so it works with global theming.

* light content status bar for dark nav bar.

* Added Step 1.

https://github.com/material-components/material-components-ios/pull/3491
2018-04-26 12:54:44 -04:00

164 lines
5.2 KiB
Objective-C

/*
Copyright 2018-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 AppBarTypicalCollectionViewExample : UICollectionViewController
// Step 1: Create an App Bar.
@property(nonatomic, strong) MDCAppBar *appBar;
@property(nonatomic, strong) MDCSemanticColorScheme *colorScheme;
@property(nonatomic, strong) MDCTypographyScheme *typographyScheme;
@end
@implementation AppBarTypicalCollectionViewExample
- (id)init {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 10.0f;
self = [super initWithCollectionViewLayout:layout];
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];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Collection view setup
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.contentInset = UIEdgeInsetsMake(4, 8, 4, 8);
[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:@"Cell"];
// Recommended step: Set the tracking scroll view.
MDCFlexibleHeaderView *headerView = self.appBar.headerViewController.headerView;
headerView.trackingScrollView = self.collectionView;
headerView.minimumHeight = 64;
headerView.maximumHeight = 200;
[self.appBar addSubviewsToParent];
[MDCAppBarTypographyThemer applyTypographyScheme:self.typographyScheme toAppBar:_appBar];
[MDCAppBarColorThemer applySemanticColorScheme:self.colorScheme toAppBar:_appBar];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return 100;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
switch (indexPath.row%3) {
case 0:
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
break;
case 1:
cell.backgroundColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
break;
case 2:
cell.backgroundColor = [UIColor colorWithWhite:0.7f alpha:1.0f];
break;
default:
break;
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGRect collectionViewFrame = collectionView.frame;
return CGSizeMake(collectionViewFrame.size.width/2.f - 14.f, 40.f);
}
#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];
}
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
@end
@implementation AppBarTypicalCollectionViewExample (CatalogByConvention)
+ (NSArray *)catalogBreadcrumbs {
return @[ @"App Bar", @"Collection View with App bar" ];
}
+ (BOOL)catalogIsPrimaryDemo {
return NO;
}
- (BOOL)catalogShouldHideNavigation {
return YES;
}
+ (BOOL)catalogIsPresentable {
return YES;
}
@end