Skip to content

Commit

Permalink
Resign first responder of searchBar when background of tableView is t…
Browse files Browse the repository at this point in the history
…apped
  • Loading branch information
myell0w committed Jan 2, 2012
1 parent b08c3dd commit 144d439
Showing 1 changed file with 18 additions and 1 deletion.
Expand Up @@ -11,6 +11,7 @@ @interface MTSlideViewController () <UITableViewDelegate, UITableViewDataSource,
BOOL rotationEnabled_;
CGPoint startingDragPoint_;
CGFloat startingDragTransformTx_;
UITapGestureRecognizer *tableViewTapGestureRecognizer_;
}

@property (nonatomic, strong, readwrite) UINavigationController *slideNavigationController;
Expand Down Expand Up @@ -105,12 +106,15 @@ - (void)viewDidLoad {
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleNavigationBarPan:)];
[slideNavigationController_.navigationBar addGestureRecognizer:panRecognizer];

tableViewTapGestureRecognizer_ = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTableViewTap:)];
tableViewTapGestureRecognizer_.enabled = NO;
[self.tableView addGestureRecognizer:tableViewTapGestureRecognizer_];

UIImage *searchBarBackground = [UIImage imageNamed:@"MTSlideViewController.bundle/search_bar_background"];
[searchBar_ setBackgroundImage:[searchBarBackground stretchableImageWithLeftCapWidth:0 topCapHeight:0]];
searchBarBackgroundView_.image = [searchBarBackground stretchableImageWithLeftCapWidth:0 topCapHeight:0];
searchBar_.placeholder = NSLocalizedString(@"Search", @"Search");


if ([self.dataSource respondsToSelector:@selector(initialSelectedIndexPathForSlideViewController:)]) {
[tableView_ selectRowAtIndexPath:[self.dataSource initialSelectedIndexPathForSlideViewController:self]
animated:NO
Expand Down Expand Up @@ -219,6 +223,10 @@ - (void)handleNavigationBarPan:(UIPanGestureRecognizer *)gestureRecognizer {
}
}

- (void)handleTableViewTap:(UITapGestureRecognizer *)gestureRecognizer {
[searchBar_ resignFirstResponder];
}

////////////////////////////////////////////////////////////////////////
#pragma mark - UITouch Handling
////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -389,17 +397,25 @@ - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

[searchBar setShowsCancelButton:YES animated:YES];
rotationEnabled_ = NO;
tableViewTapGestureRecognizer_.enabled = YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([self.dataSource respondsToSelector:@selector(slideViewController:searchTermDidChange:)]) {
[self.dataSource slideViewController:self searchTermDidChange:searchBar.text];
[tableView_ reloadData];
}

if (searchText.length == 0) {
tableViewTapGestureRecognizer_.enabled = YES;
} else {
tableViewTapGestureRecognizer_.enabled = NO;
}
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
rotationEnabled_ = YES;
tableViewTapGestureRecognizer_.enabled = NO;
[searchBar setShowsCancelButton:NO animated:YES];
}

Expand All @@ -408,6 +424,7 @@ - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self slideOutSlideNavigationControllerView];
[tableView_ reloadData];
rotationEnabled_ = YES;
tableViewTapGestureRecognizer_.enabled = NO;
[searchBar setShowsCancelButton:NO animated:YES];
}

Expand Down

0 comments on commit 144d439

Please sign in to comment.