在 UITableView 上显示 UISearchController 向下滚动
Show UISearchController on UITableView scroll down
我的 UITableView
上面有一个 UISearchController
,我的 table 有很多记录。当用户在 table 中转到底部时,他必须返回顶部才能看到搜索栏。下面你可以看到我是如何创建搜索栏的:
- (void) showSearchBar {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
//self.sharedNavigationItem.titleView = _searchController.searchBar;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
self.definesPresentationContext = YES;
_searchController.hidesNavigationBarDuringPresentation = NO;
}
我想知道如何在 table 中向下滚动(向上)时显示搜索栏,而在 table 中向上滚动(向下)时如何将其隐藏我在 table 的底部。
如果您使 header 保持在顶部,则搜索字段应该保持可见
UITableViewStylePlain: A plain table view. Any section headers or footers are displayed as inline separators and float when the table view is scrolled.
UITableViewStyleGrouped: A table view whose sections present distinct groups of rows. The section headers and footers do not float.
搜索栏可以任意放置在UI中。创建 table 视图的同级视图,例如放置在 table 上方。给那个视图一个最初设置为 44px 的高度约束,并为视图和约束提供出口...
@property(weak,nonatomic) IBOutlet UIView *searchBarContainerView;
@property(weak,nonatomic) IBOutlet NSLayoutConstraint *searchBarHeightConstraint;
现在,您的设置代码更改为:
// ...
self.searchBarHeightConstraint.constant = self.searchController.searchBar.bounds.size.height;
[self.searchBarContainerView addSubview:self.searchController.searchBar];
// ...
我正在尝试帮助您解决我所做的事情,希望对您有所帮助。
这就是我在 table 视图的 header 视图中添加标签的方式,我试图用您的搜索控制器替换它;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 40);
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor clearColor];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
[view addSubview:_searchController];
view.tag = 123;
return view;
}
如果您希望在拖动移动完成时隐藏效果,请使用以下方法;
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset{
if (velocity.y > 0){
NSLog(@"up");
[self.view viewWithTag:123].hidden = true;
}
if (velocity.y < 0){
NSLog(@"down");
[self.view viewWithTag:123].hidden = false;
}
要立即更改滚动 table 视图,请使用以下方法:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView //working delegate
{
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
NSLog(@"down");
[self.view viewWithTag:123].hidden = false;
} else {
NSLog(@"up");
[self.view viewWithTag:123].hidden = true;
}
}
经过一番搜索,我想到了一些新的答案,希望对您有所帮助!
确定滚动方向:
BOOL showSearch;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
// down
showSearch = true;
} else {
// up
showSearch = false;
}
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset{
if (showSearch) {
[self addSearch];
}
else {
[self removeSearch];
}
[_tableView reloadData];
}
添加和删除 SearchController:
-(void)addSearch {
if (!_searchController){
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_tableView.tableHeaderView = _searchController.searchBar;
_searchController.delegate = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_searchController.active = NO;
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_tableView.tableHeaderView = _searchController.searchBar;
_searchController.searchBar.delegate = self;
}
}
-(void)removeSearch {
_searchController.active = NO;
[_searchController removeFromParentViewController];
_searchController = nil;
_tableView.tableHeaderView = nil;
}
我的 UITableView
上面有一个 UISearchController
,我的 table 有很多记录。当用户在 table 中转到底部时,他必须返回顶部才能看到搜索栏。下面你可以看到我是如何创建搜索栏的:
- (void) showSearchBar {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
//self.sharedNavigationItem.titleView = _searchController.searchBar;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
self.definesPresentationContext = YES;
_searchController.hidesNavigationBarDuringPresentation = NO;
}
我想知道如何在 table 中向下滚动(向上)时显示搜索栏,而在 table 中向上滚动(向下)时如何将其隐藏我在 table 的底部。
如果您使 header 保持在顶部,则搜索字段应该保持可见
UITableViewStylePlain: A plain table view. Any section headers or footers are displayed as inline separators and float when the table view is scrolled.
UITableViewStyleGrouped: A table view whose sections present distinct groups of rows. The section headers and footers do not float.
搜索栏可以任意放置在UI中。创建 table 视图的同级视图,例如放置在 table 上方。给那个视图一个最初设置为 44px 的高度约束,并为视图和约束提供出口...
@property(weak,nonatomic) IBOutlet UIView *searchBarContainerView;
@property(weak,nonatomic) IBOutlet NSLayoutConstraint *searchBarHeightConstraint;
现在,您的设置代码更改为:
// ...
self.searchBarHeightConstraint.constant = self.searchController.searchBar.bounds.size.height;
[self.searchBarContainerView addSubview:self.searchController.searchBar];
// ...
我正在尝试帮助您解决我所做的事情,希望对您有所帮助。 这就是我在 table 视图的 header 视图中添加标签的方式,我试图用您的搜索控制器替换它;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 40);
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor clearColor];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
[view addSubview:_searchController];
view.tag = 123;
return view;
}
如果您希望在拖动移动完成时隐藏效果,请使用以下方法;
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset{
if (velocity.y > 0){
NSLog(@"up");
[self.view viewWithTag:123].hidden = true;
}
if (velocity.y < 0){
NSLog(@"down");
[self.view viewWithTag:123].hidden = false;
}
要立即更改滚动 table 视图,请使用以下方法:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView //working delegate
{
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
NSLog(@"down");
[self.view viewWithTag:123].hidden = false;
} else {
NSLog(@"up");
[self.view viewWithTag:123].hidden = true;
}
}
经过一番搜索,我想到了一些新的答案,希望对您有所帮助!
确定滚动方向:
BOOL showSearch;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
// down
showSearch = true;
} else {
// up
showSearch = false;
}
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset{
if (showSearch) {
[self addSearch];
}
else {
[self removeSearch];
}
[_tableView reloadData];
}
添加和删除 SearchController:
-(void)addSearch {
if (!_searchController){
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_tableView.tableHeaderView = _searchController.searchBar;
_searchController.delegate = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_searchController.active = NO;
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_tableView.tableHeaderView = _searchController.searchBar;
_searchController.searchBar.delegate = self;
}
}
-(void)removeSearch {
_searchController.active = NO;
[_searchController removeFromParentViewController];
_searchController = nil;
_tableView.tableHeaderView = nil;
}