开发者

making the searchbar to stay on the top

Is it possible to change the iphone contact application kind of screen to have the searchb开发者_如何学Car staying on the top always ? if yes how ?


I found out the way to do it.

here it is

  1. pull the table view to a separate view
  2. place the search bar first and then the table view in the new separate view
  3. create a iboutlet for table view and connect the same.
  4. make appropriate changes for tableview delegate.
  5. Change the measurments of the uitableview added to the new tableview.


I know it's an old question, but I found out a solution for this, which works with the classic UITableViewController and UTSearchDisplayController.

I created a container view for the searchBar 1st then put the searchbar inside it. The container must not clip to bounds. After this you can change the position of the searchbar relative to the container. One problem with this that this way the searchbar not handle user interactions. So we need to use our own container which get the events below its real frame.

Our container class:

@interface _SearchContainerView : UIView
@end

@implementation _SearchContainerView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.subviews.count > 0) {
        UISearchBar *searchBar = (UISearchBar *) self.subviews[0];
        CGRect f = searchBar.frame;
        f = CGRectMake(0, 0, f.size.width, f.origin.y + f.size.height);
        if (CGRectContainsPoint(f, point)) return YES;
    }
    return [super pointInside:point withEvent:event];
}
@end

If you create the searchBar programmatically you can set the this up with a following like code:

- (void)setSearchEnabled:(BOOL)searchEnabled {
    if (searchBar == nil && searchEnabled) {
        searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)];
        searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                                                contentsController:self];
        searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
                                     | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        searchDisplayController.delegate = self;
        searchDisplayController.searchResultsDataSource = self;

        searchContainer = [[_SearchContainerView alloc] initWithFrame:searchBar.frame];
        [container addSubview:searchBar];
        container.clipsToBounds = NO;

        self.tableView.tableHeaderView = container;

    }  else {
        [searchBar removeFromSuperview];
        self.tableView.tableHeaderView = nil;
        searchBar = nil;
        searchDisplayController = nil;
        searchContainer = nil;
    }
}

Then you can change the position based on the tableView's scroll position:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (searchBar == nil || searchDisplayController.isActive) return;
    CGRect b = self.tableView.bounds;
    // Position the searchbar to the top of the tableview
    searchBar.frame = CGRectMake(0, b.origin.y, b.size.width, 44);
}

And the last part is to restore everything after searching:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    // Restore header alpha
    searchContainer.alpha = 1.0;
    // Place the searchbar back to the tableview
    [searchBar removeFromSuperview];
    [searchContainer addSubview:searchBar];
    // Refresh position and redraw
    CGPoint co = self.tableView.contentOffset;
    [self.tableView setContentOffset:CGPointZero animated:NO];
    [self.tableView setContentOffset:co animated:NO];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜