iphone - UISearchBar 在 UITableView 滚动时滚动

标签 iphone ios uitableview scroll uisearchbar

如何实现类似Instagram.app的UISearchBar滚动效果? UISearchBar 向上滚动,而 UITableView 向上滚动。 UISearchBar 当 origin.y 等于 0 时停止向下滚动; enter image description here

enter image description here

最佳答案

我的解决方案如下:

- (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) {

                     }];
}
}

关于iphone - UISearchBar 在 UITableView 滚动时滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14973614/

相关文章:

ios - 如何导出任意一段视频?导出最后 X 秒视频时出现 "Operation Stopped"错误

ios - 如何使用 UISwipeGestureRecognizer 滑动以滑入并刷新相同的 uitableview

iphone - 如何将静态广告横幅添加到 UITableViewController 中?

IOS 分组 TableView 透明单元格

iphone - 通过检查数组计数来防止数组索引崩溃,是否存在性能问题?

ios - 样式化现有的 UITextField

android - React Native - 选择多种样式

ios - Table View 窗外屏幕 iOS objective C

iphone - iAd 出现和消失

iphone - 如何在 iPhone 上的 uinavigationbar 上创建信息按钮