UISearchDisplayController 没有完全覆盖子视图控制器
Posted
技术标签:
【中文标题】UISearchDisplayController 没有完全覆盖子视图控制器【英文标题】:UISearchDisplayController does not entirely cover a child view controller 【发布时间】:2014-02-05 23:43:22 【问题描述】:我有一个根视图控制器,它由顶部的搜索栏和底部的子表视图控制器组成。由于以下原因,我使用了组合而不是将搜索栏分配给表格视图的标题:
-
我不希望索引与搜索栏(如通讯录应用)重叠。
我希望搜索栏具有粘性。也就是说,当我滚动表格视图时它不会移动(再次像联系人应用程序一样)。
我的表格视图已经有了标题。
由于搜索栏在根视图控制器中,我也在根视图控制器中实例化了我的搜索显示控制器。我寻求建议的搜索 UI 存在两个问题:
-
半透明的灰色覆盖不会覆盖整个子表视图。它使标题的顶部和索引可见。
同样,搜索结果表不会覆盖整个子表视图。我知道如何手动更改此结果表视图的框架,但这样做只能解决这个问题……灰色半透明覆盖的框架未链接到结果表视图框架。它们不是访问叠加层的属性。
1) 空闲
2) 进入搜索栏
3) 开始输入
#import "ContactsRootViewController.h"
#import "ContactsViewController.h"
#import "UIView+position.h"
#import "User.h"
#import "UserCellView.h"
#import "UserViewController.h"
@interface ContactsRootViewController ()
@property(nonatomic, strong) UISearchBar* searchBar;
@property(nonatomic, strong) ContactsViewController* contactsViewController;
@property(nonatomic, strong) UISearchDisplayController* searchController;
@property(nonatomic, strong) NSMutableArray* matchedUsers;
@end
@implementation ContactsRootViewController
#pragma mark UIViewController
- (NSString*)title
return @"Contacts";
- (void)viewDidLoad
[super viewDidLoad];
self.matchedUsers = [NSMutableArray array];
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.placeholder = @"Search";
[self.searchBar sizeToFit];
[self.view addSubview:self.searchBar];
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
if (self.contactsViewController == nil)
self.contactsViewController = [[ContactsViewController alloc] init];
[self addChildViewController:self.contactsViewController];
self.contactsViewController.view.frame = CGRectMake(
0.0,
self.searchBar.bottomY,
self.view.frame.size.width,
self.view.frame.size.height - self.searchBar.bottomY
);
self.contactsViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:self.contactsViewController.view];
[self.contactsViewController didMoveToParentViewController:self];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self.contactsViewController];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return self.matchedUsers.count;
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString* identifier = @"contactsRootViewUserCell";
UserCellView* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
cell = [[UserCellView alloc] initWithIdentifier:identifier];
cell.user = [self.matchedUsers objectAtIndex:indexPath.row];
return cell;
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[self.navigationController pushViewController:[[UserViewController alloc] initWithUser:[self.matchedUsers objectAtIndex:indexPath.row]] animated:YES];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return [UserCellView height];
#pragma mark UISearchDisplayControllerDelegate
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString *)searchString
[self.matchedUsers removeAllObjects];
searchString = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (searchString.length > 0)
for (User* user in self.contactsViewController.allUsers)
NSRange match = [user.userDisplayName rangeOfString:searchString options:NSCaseInsensitiveSearch];
if (match.location != NSNotFound)
[self.matchedUsers addObject:user];
return YES;
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
[self.searchBar resignFirstResponder];
@end
【问题讨论】:
【参考方案1】:我重新实现了 UISearchDisplayController,调用了我的实现 SearchController。它做同样的事情并有类似的委托回调,但搜索结果的框架可以由程序员控制。
标题
#import <Foundation/Foundation.h>
@class SearchController;
@protocol SearchControllerDelegate <NSObject>
@required
- (BOOL)searchController:(SearchController*)controller shouldReloadTableForSearchString:(NSString*)searchText;
@optional
- (void)searchController:(SearchController*)controller didShowSearchResultsTableView:(UITableView*)tableView;
- (void)searchController:(SearchController *)controller didHideSearchResultsTableView:(UITableView *)tableView;
- (void)searchControllerDidBeginSearch:(SearchController*)controller;
- (void)searchControllerDidEndSearch:(SearchController*)controller;
@end
@interface SearchController : UIViewController <UISearchBarDelegate>
@property(nonatomic, weak) NSObject<SearchControllerDelegate>* delegate;
@property(nonatomic, weak) NSObject<UITableViewDataSource>* searchResultsDataSource;
@property(nonatomic, weak) NSObject<UITableViewDelegate>* searchResultsDelegate;
@property(nonatomic, strong, readonly) UITableView* searchResultsTableView;
- (id)initWithSearchBar:(UISearchBar*)searchBar;
@end
实施
#import "SearchController.h"
#import "UIView+position.h"
@interface SearchController ()
@property(nonatomic, strong) UISearchBar* searchBar;
@property(nonatomic, strong) UIButton* searchResultsVeil;
@property(nonatomic, strong, readwrite) UITableView* searchResultsTableView;
@property(nonatomic, assign) BOOL searchResultsTableViewHidden;
- (void)didTapSearchResultsVeil;
- (void)hideSearchResults;
@end
@implementation SearchController
#pragma mark UIViewController
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
[self.searchResultsTableView deselectRowAtIndexPath:[self.searchResultsTableView indexPathForSelectedRow] animated:YES];
- (void)viewDidLoad
[super viewDidLoad];
self.view.userInteractionEnabled = NO;
#pragma mark SearchController ()
- (void)hideSearchResults
self.searchBar.text = nil;
[self.searchResultsTableView reloadData];
self.searchResultsTableViewHidden = YES;
[self.searchBar resignFirstResponder];
- (void)didTapSearchResultsVeil
[self hideSearchResults];
- (void)setSearchResultsTableViewHidden:(BOOL)searchResultsTableViewHidden
if (self.searchResultsTableView != nil)
if (self.searchResultsTableView.hidden && !searchResultsTableViewHidden)
self.searchResultsTableView.hidden = searchResultsTableViewHidden;
if ([self.delegate respondsToSelector:@selector(searchController:didShowSearchResultsTableView:)])
[self.delegate searchController:self didShowSearchResultsTableView:self.searchResultsTableView];
else if (!self.searchResultsTableView.hidden && searchResultsTableViewHidden)
self.searchResultsTableView.hidden = searchResultsTableViewHidden;
if ([self.delegate respondsToSelector:@selector(searchController:didHideSearchResultsTableView:)])
[self.delegate searchController:self didHideSearchResultsTableView:self.searchResultsTableView];
- (BOOL)searchResultsTableViewHidden
return self.searchResultsTableView == nil || self.searchResultsTableView.hidden;
#pragma mark SearchController
- (id)initWithSearchBar:(UISearchBar *)searchBar
if (self = [super init])
self.searchBar = searchBar;
self.searchBar.delegate = self;
return self;
- (void)setSearchResultsDataSource:(NSObject<UITableViewDataSource> *)searchResultsDataSource
_searchResultsDataSource = searchResultsDataSource;
if (self.searchResultsTableView != nil)
self.searchResultsTableView.dataSource = searchResultsDataSource;
- (void)setSearchResultsDelegate:(NSObject<UITableViewDelegate> *)searchResultsDelegate
_searchResultsDelegate = searchResultsDelegate;
if (self.searchResultsTableView != nil)
self.searchResultsTableView.delegate = searchResultsDelegate;
#pragma mark UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
if ([self.delegate searchController:self shouldReloadTableForSearchString:searchText])
[self.searchResultsTableView reloadData];
self.searchResultsTableViewHidden = [self.searchResultsTableView.dataSource tableView:self.searchResultsTableView numberOfRowsInSection:0] == 0;
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
[searchBar setShowsCancelButton:YES animated:YES];
if (self.searchResultsVeil == nil)
self.searchResultsVeil = [[UIButton alloc] initWithFrame:self.view.bounds];
self.searchResultsVeil.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
self.searchResultsVeil.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.searchResultsVeil addTarget:self action:@selector(didTapSearchResultsVeil) forControlEvents:UIControlEventTouchUpInside];
self.searchResultsTableView = [[UITableView alloc] initWithFrame:self.searchResultsVeil.bounds style:UITableViewStylePlain];
self.searchResultsTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if ([self.searchResultsTableView respondsToSelector:@selector(setSeparatorInset:)])
self.searchResultsTableView.separatorInset = UIEdgeInsetsMake(
0.0,
self.searchResultsTableView.width,
0.0,
0.0
);
self.searchResultsTableViewHidden = YES;
if (self.searchResultsDataSource != nil)
self.searchResultsTableView.dataSource = self.searchResultsDataSource;
if (self.searchResultsDelegate != nil)
self.searchResultsTableView.delegate = self.searchResultsDelegate;
[self.view addSubview:self.searchResultsVeil];
[self.searchResultsVeil addSubview:self.searchResultsTableView];
self.view.userInteractionEnabled = YES;
self.searchResultsVeil.hidden = NO;
if ([self.delegate respondsToSelector:@selector(searchControllerDidBeginSearch:)])
[self.delegate searchControllerDidBeginSearch:self];
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
[searchBar setShowsCancelButton:NO animated:YES];
self.view.userInteractionEnabled = NO;
self.searchResultsVeil.hidden = YES;
if ([self.delegate respondsToSelector:@selector(searchControllerDidEndSearch:)])
[self.delegate searchControllerDidEndSearch:self];
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
[self hideSearchResults];
@end
用法
self.searchController = [[SearchController alloc] initWithSearchBar:self.searchBar];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
[self addChildViewController:self.searchController];
self.searchController.view.frame = CGRectMake(
self.searchBar.x,
self.searchBar.bottomY,
self.searchBar.width,
self.view.height - self.searchBar.bottomY
);
[self.view addSubview:self.searchController.view];
[self.searchController didMoveToParentViewController:self];
【讨论】:
【参考方案2】:看起来您的视图控制器没有定义演示上下文。我有一个类似的问题,并能够通过设置解决它
self.definesPresentationContext = YES;
在viewDidLoad
。根据文档,这个属性是
一个布尔值,指示当视图控制器或其后代之一呈现视图控制器时是否覆盖此视图控制器的视图。
【讨论】:
以上是关于UISearchDisplayController 没有完全覆盖子视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
为啥 UISearchDisplayController 有时有效,有时无效?
iOS UISearchDisplayController学习笔记
在 UISearchDisplayController 上遇到僵尸问题
修复 UITableView 顶部的 UISearchdisplaycontroller 搜索栏