Skip to content

Commit

Permalink
Merge pull request #68 from gskbyte/support-ios11
Browse files Browse the repository at this point in the history
Fix iOS 11 issues and bump version to 1.0.2
  • Loading branch information
gskbyte authored Oct 19, 2017
2 parents a355553 + 084271a commit f99766f
Show file tree
Hide file tree
Showing 20 changed files with 257 additions and 96 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# [1.0.2](https://github.com/gskbyte/GSKStretchyHeaderView/releases/tag/1.0.2)

- [Fix device rotation issue](https://github.com/gskbyte/GSKStretchyHeaderView/pull/65)
- [Add Xcode 9 support](https://github.com/gskbyte/GSKStretchyHeaderView/pull/64)
- [Fix contentInset and view hierarchy issues with iOS 11](https://github.com/gskbyte/GSKStretchyHeaderView/pull/68)


Thanks [@julienpouget](https://github.com/julienpouget)!

# [1.0.1](https://github.com/gskbyte/GSKStretchyHeaderView/releases/tag/1.0.1)

- [Fixed crash when removing header from a superview without window](https://github.com/gskbyte/GSKStretchyHeaderView/pull/53).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "GSKExampleBaseTableViewController.h"
@import UIKit;

@interface GSKAirbnbExampleViewController : GSKExampleBaseTableViewController
@interface GSKAirbnbExampleViewController : UICollectionViewController

@end
57 changes: 48 additions & 9 deletions Example/GSKStretchyHeaderView/GSKAirbnbExampleViewController.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#import "GSKAirbnbExampleViewController.h"
#import "GSKAirbnbStretchyHeaderView.h"
#import "GSKExampleDataSource.h"

@interface GSKAirbnbExampleViewController () <GSKAirbnbStretchyHeaderViewDelegate>
@property (nonatomic, readonly) UICollectionViewFlowLayout *collectionViewFlowLayout;
@property (nonatomic) GSKStretchyHeaderView *stretchyHeaderView;
@property (nonatomic) GSKExampleDataSource *dataSource;
@end

@interface GSKPresentableViewController : UIViewController
Expand All @@ -10,18 +14,44 @@ @interface GSKPresentableViewController : UIViewController

@implementation GSKAirbnbExampleViewController

- (instancetype)init {
UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
collectionViewFlowLayout.minimumLineSpacing = 0;
collectionViewFlowLayout.minimumInteritemSpacing = 0;

return [super initWithCollectionViewLayout:collectionViewFlowLayout];
}

- (UICollectionViewFlowLayout *)collectionViewFlowLayout {
return (UICollectionViewFlowLayout *)self.collectionViewLayout;
}

- (void)viewDidLoad {
[super viewDidLoad];

self.collectionView.backgroundColor = [UIColor whiteColor];
if (@available(iOS 11.0, *)) {
self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(beginRefreshing:) forControlEvents:UIControlEventValueChanged];

[self.tableView addSubview:self.refreshControl];
self.stretchyHeaderView = [self loadStretchyHeaderView];
[self.collectionView addSubview:self.stretchyHeaderView];

self.dataSource = [[GSKExampleDataSource alloc] init];
[self.dataSource registerForCollectionView:self.collectionView];

self.dataSource.numberOfSections = 10;
self.dataSource.numberOfRowsInEverySection = 6;
self.dataSource.displaysSectionHeaders = YES;
self.dataSource.cellColors = @[[UIColor whiteColor], [UIColor lightGrayColor]];

self.collectionView.refreshControl = [[UIRefreshControl alloc] init];
[self.collectionView.refreshControl addTarget:self action:@selector(beginRefreshing:) forControlEvents:UIControlEventValueChanged];
}

- (GSKStretchyHeaderView *)loadStretchyHeaderView {
CGRect frame = CGRectMake(0, 0, self.tableView.frame.size.width, 250);
CGRect frame = CGRectMake(0, 0, self.collectionView.frame.size.width, 250);
GSKAirbnbStretchyHeaderView *headerView = [[GSKAirbnbStretchyHeaderView alloc] initWithFrame:frame];
headerView.maximumContentHeight = 300;
headerView.minimumContentHeight = 84;
Expand All @@ -42,10 +72,10 @@ - (void)airbnbStretchyHeaderView:(GSKAirbnbStretchyHeaderView *)headerView

- (void)airbnbStretchyHeaderView:(GSKAirbnbStretchyHeaderView *)headerView
didTapSearchButton:(id)sender {
CGPoint contentOffset = self.tableView.contentOffset;
CGPoint contentOffset = self.collectionView.contentOffset;
if (contentOffset.y < -self.stretchyHeaderView.minimumContentHeight) {
contentOffset.y = -self.stretchyHeaderView.minimumContentHeight;
[self.tableView setContentOffset:contentOffset animated:YES];
[self.collectionView setContentOffset:contentOffset animated:YES];
}
}

Expand All @@ -59,13 +89,22 @@ - (void)airbnbStretchyHeaderView:(GSKAirbnbStretchyHeaderView *)headerView didTa

- (void)beginRefreshing:(id)sender {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.refreshControl endRefreshing];
[self.collectionView.refreshControl endRefreshing];
});
}

@end
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(collectionView.frame.size.width,
[self.dataSource heightForItemAtIndexPath:indexPath]);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(collectionView.frame.size.width, 20);
}

@end

@implementation GSKPresentableViewController

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#import "GSKExampleBaseTableViewController.h"
#import "UINavigationController+Transparency.h"

static const NSUInteger kNumberOfRows = 100;

@interface GSKExampleBaseTableViewController ()

@end
Expand All @@ -21,6 +19,9 @@ - (void)viewDidLoad {
[super viewDidLoad];

self.automaticallyAdjustsScrollViewInsets = NO;
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
UIEdgeInsets contentInset = self.tableView.contentInset;
if (self.navigationController) {
contentInset.top = 64;
Expand Down Expand Up @@ -52,7 +53,7 @@ - (GSKStretchyHeaderView *)loadStretchyHeaderView {
}

- (GSKExampleDataSource *)loadDataSource {
return [[GSKExampleDataSource alloc] initWithNumberOfRows:kNumberOfRows];
return [[GSKExampleDataSource alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

NS_ASSUME_NONNULL_BEGIN

static const NSUInteger kNumberOfRows = 100;

@interface GSKExampleCollectionViewController () <UICollectionViewDelegateFlowLayout>
@property (nonatomic) GSKExampleData *data;
@property (nonatomic) GSKStretchyHeaderView *stretchyHeaderView;
Expand All @@ -33,6 +31,9 @@ - (void)viewDidLoad {
[super viewDidLoad];

self.collectionView.backgroundColor = [UIColor whiteColor];
if (@available(iOS 11.0, *)) {
self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

if (self.data.headerViewClass) {
self.stretchyHeaderView = [[self.data.headerViewClass alloc] initWithFrame:CGRectMake(0, 0, self.collectionView.frame.size.width, self.data.headerViewInitialHeight)];
Expand All @@ -44,7 +45,7 @@ - (void)viewDidLoad {
}
[self.collectionView addSubview:self.stretchyHeaderView];

self.dataSource = [[GSKExampleDataSource alloc] initWithNumberOfRows:kNumberOfRows];
self.dataSource = [[GSKExampleDataSource alloc] init];
[self.dataSource registerForCollectionView:self.collectionView];
}

Expand Down
10 changes: 8 additions & 2 deletions Example/GSKStretchyHeaderView/GSKExampleDataSource.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#import <Foundation/Foundation.h>

static const NSUInteger kDefaultNumberOfRows = 100;

@interface GSKExampleDataSource : NSObject<UITableViewDataSource,
UICollectionViewDataSource>

@property (nonatomic, readonly) NSUInteger numberOfRows;
// set to true to display basic titles: "Section #X"
@property (nonatomic) BOOL displaysSectionHeaders; // default value: false
@property (nonatomic) NSUInteger numberOfSections; // default value: 1
@property (nonatomic) NSUInteger numberOfRowsInEverySection;
@property (nonatomic) NSArray<UIColor *> *cellColors;

- (instancetype)initWithNumberOfRows:(NSUInteger)numberOfRows;
- (instancetype)init;
- (void)registerForTableView:(UITableView *)tableView;
- (void)registerForCollectionView:(UICollectionView *)collectionView;
- (CGFloat)heightForItemAtIndexPath:(NSIndexPath *)indexPath;
- (NSString *)titleForSection:(NSInteger)section;

@end
82 changes: 73 additions & 9 deletions Example/GSKStretchyHeaderView/GSKExampleDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ @interface GSKExampleDataSource ()
@property (nonatomic) NSMutableArray<NSNumber *> *rowHeights;
@end

@interface GSKAirbnbSectionTitleView: UICollectionReusableView
@property (nonatomic) UILabel *label;
@end

@implementation GSKExampleDataSource

- (instancetype)initWithNumberOfRows:(NSUInteger)numberOfRows {
- (instancetype)init {
self = [super init];
if (self) {
_numberOfRows = numberOfRows;
self.rowHeights = [NSMutableArray arrayWithCapacity:numberOfRows];
for (NSUInteger i = 0; i < numberOfRows; ++i) {
CGFloat height = 40 + arc4random_uniform(160);
[self.rowHeights addObject:@(height)];
}
_displaysSectionHeaders = false;
_numberOfSections = 1;
_numberOfRowsInEverySection = kDefaultNumberOfRows;
[self updateRowHeights];
self.cellColors = @[[UIColor grayColor], [UIColor lightGrayColor]];
}
return self;
}

- (void)setNumberOfRowsInEverySection:(NSUInteger)numberOfRowsInEverySection {
_numberOfRowsInEverySection = numberOfRowsInEverySection;
[self updateRowHeights];
}

- (void)registerForTableView:(UITableView *)tableView {
_scrollView = tableView;

Expand All @@ -33,14 +40,32 @@ - (void)registerForTableView:(UITableView *)tableView {
- (void)registerForCollectionView:(UICollectionView *)collectionView {
collectionView.dataSource = self;
[GSKCollectionViewCell registerIn:collectionView];
[collectionView registerClass:[GSKAirbnbSectionTitleView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:NSStringFromClass([GSKAirbnbSectionTitleView class])];
}

- (void)updateRowHeights {
self.rowHeights = [NSMutableArray arrayWithCapacity:self.numberOfRowsInEverySection];
for (NSUInteger i = 0; i < self.numberOfRowsInEverySection; ++i) {
CGFloat height = 40 + arc4random_uniform(160);
[self.rowHeights addObject:@(height)];
}
}

#pragma mark - table view

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.numberOfRows;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self titleForSection:section];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.numberOfRowsInEverySection;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GSKTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[GSKTableViewCell reuseIdentifier]];
Expand All @@ -50,6 +75,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

#pragma mark - collectionView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return self.numberOfSections;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.rowHeights.count;
}
Expand All @@ -61,8 +90,43 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if (!self.displaysSectionHeaders) {
return nil;
}

GSKAirbnbSectionTitleView *titleView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:NSStringFromClass([GSKAirbnbSectionTitleView class])
forIndexPath:indexPath];
titleView.label.text = [self titleForSection:indexPath.section];
return titleView;
}

#pragma mark - generic

- (CGFloat)heightForItemAtIndexPath:(NSIndexPath *)indexPath {
return [self.rowHeights[indexPath.item] floatValue];
}

- (NSString *)titleForSection:(NSInteger)section {
return self.displaysSectionHeaders ? [NSString stringWithFormat:@"Section #%@", @(section)] : nil;
}

@end


@implementation GSKAirbnbSectionTitleView

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.label = [[UILabel alloc] initWithFrame:self.bounds];
self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:self.label];

self.backgroundColor = [UIColor grayColor];
}
return self;
}

@end
6 changes: 2 additions & 4 deletions Example/GSKStretchyHeaderView/GSKExampleListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ - (void)viewDidLoad {
[super viewDidLoad];

self.title = @"GSKStretchyHeaderView";
self.tableView.rowHeight = [GSKExampleDataCell height];

[GSKExampleDataCell registerIn:self.tableView];

GSKExampleData *airbnb = [GSKExampleData dataWithTitle:@"airbnb-like header view"
Expand Down Expand Up @@ -73,10 +75,6 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
return self.exampleDatas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [GSKExampleDataCell height];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GSKExampleDataCell *cell = [tableView dequeueReusableCellWithIdentifier:[GSKExampleDataCell reuseIdentifier]];
cell.data = self.exampleDatas[indexPath.row];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ - (void)viewDidLoad {

NSMutableArray *dataSources = [NSMutableArray array];
for (NSUInteger i = 0; i < self.stretchyHeaderView.tabsCount; ++i) {
GSKExampleDataSource *dataSource = [[GSKExampleDataSource alloc] initWithNumberOfRows:100];
GSKExampleDataSource *dataSource = [[GSKExampleDataSource alloc] init];
[dataSources addObject:dataSource];
}
self.dataSources = [dataSources copy];
Expand Down
Loading

0 comments on commit f99766f

Please sign in to comment.