Ios 隐藏UISearchBar直到用户滚动?
我在我的表视图顶部实现了一个搜索栏和搜索显示控制器 当视图加载时,搜索栏和相对范围始终可见Ios 隐藏UISearchBar直到用户滚动?,ios,ios6,uisearchbar,Ios,Ios6,Uisearchbar,我在我的表视图顶部实现了一个搜索栏和搜索显示控制器 当视图加载时,搜索栏和相对范围始终可见 是否有一种简单的方法可以隐藏它,直到用户向下滚动,就像音乐应用程序中发生的那样?您需要添加搜索栏作为表视图的标题,然后将viewDidLoad中表视图的contentoffset属性设置为 [self.tableView setContentOffset:CGPointMake(0,44) animated:YES];//or (0, 88) depends on the height of it 对于
是否有一种简单的方法可以隐藏它,直到用户向下滚动,就像音乐应用程序中发生的那样?您需要添加搜索栏作为表视图的标题,然后将
viewDidLoad
中表视图的contentoffset
属性设置为
[self.tableView setContentOffset:CGPointMake(0,44) animated:YES];//or (0, 88) depends on the height of it
对于搜索显示控制器,您也可以尝试此操作
[self.searchDisplayController setActive:NO animated:YES];
没有硬代码的另一种方法
[self.tableView setContentOffset:CGPointMake(0.0, self.tableView.tableHeaderView.frame.size.height) animated:YES];
对于iOS 7并使用
UINavigationController
[self.tableView setContentOffset:CGPointMake(0, self.searchBar.height + self.navigationController.navigationBar.height)];
我在中重新加载tableview的最佳方式
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self performSelector:@selector(reload:) withObject:nil afterDelay:0.0];
self.edgesForExtendedLayout = UIRectEdgeNone;
}
- (void)reload:(__unused id)sender {
[self.searchDisplayController setActive:YES];
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} else {
[self.refreshControl endRefreshing];
[[[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
}
谢谢你拉了几个小时的头发,效果不错
- 在iOS 9上
- 不取决于表中的行数
- 具有可变高度的行
考虑到搜索栏是tableView.tableHeaderView
@interface MyTableViewController () @property (nonatomic, assign) BOOL firstLayout; @end - (void) viewDidLoad { [super viewDidLoad]; self.firstLayout = YES; //your code } -(void)viewDidLayoutSubviews{ [super viewDidLayoutSubviews]; if(self.firstLayout){ CGPoint offset = CGPointMake(0, self.tableView.tableHeaderView.frame.size.height - self.tableView.contentInset.top); [self.tableView setContentOffset:offset]; self.firstLayout = NO; } }
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^{
self.tableView.contentOffset = CGPointMake(0, -20);
});
}
当表中的所有行都显示在屏幕上时,解决方案不会隐藏搜索栏。对于这种类型的场景,有任何已知的解决方法吗?@Zorayr:您是否找到了一种适用于表格的工作解决方案,其中单元格可以放在整个屏幕中?有同样的问题。彻底性大+1。