4

如何实现UISearchBar类似 Instagram.app 的滚动效果?UISearchBar向上滚动时 向上UITableView滚动。当UISearchBarorigin.y 等于 0 时停止向下滚动; 在此处输入图像描述

在此处输入图像描述

4

2 回答 2

3

我的解决方案如下:

- (void)viewDidLoad
{
[super viewDidLoad];
self.myTableview.delegate = self;
self.myTableview.dataSource = self;
self.mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, searchBarHeight)];
[self.myTableView setContentInset:UIEdgeInsetsMake(searchBarHeight, 0, 0, 0)];
[self.myTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint offset = scrollView.contentOffset;
float move = offset.y + searchBarHeight;
if (move > -2*searchBarHeight && move <3*searchBarHeight) {
    [UIView animateWithDuration:0.1
                          delay:0
                        options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         CGRect searchBarFrame = self.mySearchBar.frame;
                         searchBarFrame.origin.y = MIN(MAX(-move, -searchBarHeight), 0);

                         [self.mySearchBar setFrame:searchBarFrame];

                     }

                     completion:^(BOOL f) {

                     }];
}
}
于 2013-02-20T12:42:50.473 回答
1

创建一个 UISearchBar 实例并将其设置为 tableView Header View

        [tableView setTableHeaderView:yourSearchBar];

它现在将使用 tableView 滚动

于 2013-02-20T06:39:50.543 回答