【发布时间】:2014-05-05 19:29:38
【问题描述】:
我正在 UITableView 的顶部实现 UISearchBar。
在我的 ViewDidLoad 中,我设置了 self.edgesForExtendedLayout = UIRectEdgeNone。
以下是我添加 UISearchBar 的方式。我只是在 UITableView 的标题中添加一个 UISearchBar 并调整内容偏移量:
// Table View
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.screenWidth, self.screenHeight)];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Customize table appearance
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
self.tableView.backgroundColor = [UIColor tableBackgroundColor];
// Define the table's datasource
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
// Add a search bar to the header
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.screenWidth, 44)];
self.searchBar.tintColor = [UIColor primaryBrandedColor];
self.searchBar.placeholder = NSLocalizedString(@"Search", "Search placeholder");
self.searchBar.backgroundImage = [UIImage imageNamed:@"searchBarBackground"];
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"searchBarBackground"] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
self.searchBar.barTintColor = [UIColor clearColor];
self.searchBar.delegate = self;
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Whitney-Light" size:15]];
//Setup search display controller
self.searchController = [[HUMSearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// Add the searchBar and offset the table view so it's hidden by default.
self.tableView.tableHeaderView = self.searchBar;
self.tableView.contentOffset = CGPointMake(0.0, self.searchBar.frame.size.height);
这里看起来并没有什么不寻常的地方,但是当 SearchDisplayController 显示时,原始 tableview 上有 20px 的间隙。在此示例中,我将 SearchDisplayController 子类化为不隐藏 NavigationBar 以更清楚地了解正在发生的事情。
转换的视频截图:http://cl.ly/0y3L0Z1R1I0c/20pixels.mov
我尝试过的事情:
- 在 searchDisplayControllerWillBeginSearch 上更改我的 UITableView 的框架,将其向上移动 20 像素。这打破了过渡。 http://cl.ly/033q0L3G0p1T/origin.mov
- 在 searchDisplayControllerWillBegin 上更改 UITableView 的滚动偏移量。同样,这也会中断过渡。
- 尝试手动为 UISearchBar 设置动画,但我也无法让它工作。
有什么想法吗?
【问题讨论】:
标签: ios objective-c uisearchbar uisearchdisplaycontroller