【发布时间】:2014-01-07 13:44:15
【问题描述】:
我遇到了一个搜索栏问题,当它变成 firstResponder 并退出时,它的行为方式很奇怪。
搜索栏被添加为表格视图的标题
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
self.searchBar.translucent = NO;
self.searchBar.barTintColor = [UIColor grayColor];
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
contentsController:self];
self.searchController.searchResultsDataSource = self;
视图控制器设置了JASidePanelController的左侧面板,当键盘显示或隐藏时它隐藏了中心面板:
- (void)keyboardWillAppear:(NSNotification *)note
{
[self.sidePanelController setCenterPanelHidden:YES
animated:YES
duration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
self.searchBar.showsCancelButton = YES;
}
- (void)keyboardWillDisappear:(NSNotification *)note
{
[self.sidePanelController setCenterPanelHidden:NO
animated:YES
duration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
self.searchBar.showsCancelButton = NO;
}
正常状态
当搜索栏变成firstResponder 时,它要么向上移动一个点,要么随机向下移动一个点
并且当搜索栏退出时,它会向上移动以到达窗口原点,然后返回其自然框架
这里有一个sample project 重现了这个错误。
编辑:
根据@kwylezsuggestion,可以通过以下方式避免搜索栏在退出时制作的不需要的动画:
self.searchBar.clipsToBounds = YES;
【问题讨论】:
-
添加您的搜索栏作为 viewcontroller 视图的子视图,而不是作为 tableView 的 tableHeader 修复向上/向下运动,如果它不必是 tableHeader。
-
是的,但我在表格视图后面有内容需要在用户滚动时显示...如果我没有找到任何东西,我可能会退回到解决方案。
-
试试这个 self.searchBar.showsCancelButton = NO;[self.searchBar sizeToFit];
标签: ios uisearchbar uisearchdisplaycontroller