UITearchBar在UITableView滚动时滚动

时间:2013-02-20 06:30:11

标签: iphone ios uitableview scroll uisearchbar

如何完成像Instagram.app这样的UISearchBar滚动效果?当UISearchBar向上滚动时,UITableView向上滚动。 UISearchBar停止向下滚动它的原点.y等于0;  enter image description here

enter image description here

2 个答案:

答案 0 :(得分: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) {

                     }];
}
}

答案 1 :(得分:1)

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

        [tableView setTableHeaderView:yourSearchBar];

现在将使用tableView

滚动