Skip to content

Commit

Permalink
SnappingViewController is UITableViewController subclass…
Browse files Browse the repository at this point in the history
- added autorotation support
- cleaned up
  • Loading branch information
bhr committed May 15, 2014
1 parent 0fa8999 commit d99ca93
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
4 changes: 1 addition & 3 deletions BHRSnappingTableView/BHRSnappingTableViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#import <UIKit/UIKit.h>
#import "BHRSnappingTableView.h"

@interface BHRSnappingTableViewController : UIViewController

@property (nonatomic, strong, readonly) BHRSnappingTableView *tableView;
@interface BHRSnappingTableViewController : UITableViewController

@end
65 changes: 36 additions & 29 deletions BHRSnappingTableView/BHRSnappingTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

@interface BHRSnappingTableViewController () <UIScrollViewDelegate, UITableViewDelegate>

@property (nonatomic, strong) BHRSnappingTableView *tableView;

@property (nonatomic, assign) BOOL resetContentOffset;
@property (nonatomic, assign) CGPoint previousTargetContentOffset;

@end
Expand All @@ -21,14 +20,45 @@ @implementation BHRSnappingTableViewController

#pragma mark - UIViewController overrides

- (void) loadView
{
BHRSnappingTableView* snappingTableView = [[BHRSnappingTableView alloc]initWithFrame:CGRectZero
style:UITableViewStylePlain];

snappingTableView.delegate = self;

self.view = snappingTableView;
self.tableView = snappingTableView;
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.resetContentOffset = YES;
}

- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];

CGPoint defaultContentOffset = CGPointMake(0.0f,
CGRectGetHeight(self.tableView.tableHeaderView.bounds));
self.previousTargetContentOffset = defaultContentOffset;
[self.tableView setContentOffset:defaultContentOffset];
CGFloat tableViewHeaderHeight = CGRectGetHeight(self.tableView.tableHeaderView.bounds);
if (self.resetContentOffset &&
self.tableView.contentOffset.y < tableViewHeaderHeight)
{
CGPoint defaultContentOffset = CGPointMake(0.0f,
tableViewHeaderHeight);
self.previousTargetContentOffset = defaultContentOffset;
[self.tableView setContentOffset:defaultContentOffset];
self.resetContentOffset = NO;
}
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation
duration:duration];

self.resetContentOffset = YES;
}

#pragma mark - UIScrollViewDelegate
Expand Down Expand Up @@ -80,27 +110,4 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
*targetContentOffset = newOffset;
}


- (BHRSnappingTableView *)tableView
{
if (!_tableView)
{
_tableView = [[BHRSnappingTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.translatesAutoresizingMaskIntoConstraints = NO;
_tableView.delegate = self;

[self.view addSubview:_tableView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_tableView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tableView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_tableView)]];
}

return _tableView;
}

@end
3 changes: 2 additions & 1 deletion BHRSnappingTableView/BHRViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ - (void)viewDidLoad
{
[super viewDidLoad];

self.edgesForExtendedLayout = UIRectEdgeNone;
self.itemsCount = kLowNumberOfItems;

[self _setUpTable];
Expand Down Expand Up @@ -85,7 +86,7 @@ - (UIView *)headerView
action:@selector(changeNumberOfItems:)
forControlEvents:UIControlEventTouchUpInside];

[_headerView addSubview:addButton];
[_headerView addSubview:addButton];
[_headerView addConstraint:[NSLayoutConstraint constraintWithItem:_headerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
Expand Down

0 comments on commit d99ca93

Please sign in to comment.