SearchDisplayController SearchBar重叠导航栏并调整自身的大小IOS7

我的应用程序有一个tableview控制器,以表单forms呈现。

在tableviewcontoller的顶部有一个UView,在UIView里面有一个导航栏和一个搜索栏。

一切都运行良好的旧版IOS,但在IOS7用户点击搜索栏时,一切都搞砸了。

正常: 在此处输入图像描述

当用户开始输入时: 在此处输入图像描述

结束后: 在此处输入图像描述

in.h中

UITableViewController @property (nonatomic,weak) IBOutlet UINavigationBar *topBar; 

尝试了一些事情,但代码似乎没有改变任何东西,当它放入一个断点,它进入委托方法虽然

in.m

 //-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { // if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { // CGRect statusBarFrame = self.topBar.frame; // [UIView animateWithDuration:0.25 animations:^{ // for (UIView *subview in self.tableView.subviews) // subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height+50); // }]; // } //} // //-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { // if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { // [UIView animateWithDuration:0.25 animations:^{ // for (UIView *subview in self.tableView.subviews) // subview.transform = CGAffineTransformIdentity; // }]; // } //} - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { CGRect frame = self.searchDisplayController.searchBar.frame; frame.origin.y += self.topBar.frame.size.height; self.searchDisplayController.searchBar.frame = frame; } } - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { CGRect statusBarFrame = self.topBar.frame; CGRect frame = self.searchDisplayController.searchBar.frame; frame.origin.y -= statusBarFrame.size.height; self.searchDisplayController.searchBar.frame= frame; } } #Additional Info - (void)viewDidLoad { [super viewDidLoad]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { //self.searchDisplayController.searchBar.searchBarStyle= UISearchBarStyleProminent; self.edgesForExtendedLayout = UIRectEdgeNone; //self.edgesForExtendedLayout = UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight; } } 

在此处输入图像描述

根据断点框架的位置和大小是正确的但它们不会改变self.searchDisplayController.searchBar.frame at all

我也试过了

 -(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { [self.topBar setHidden:YES]; } } -(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { [self.searchDisplayController.searchBar removeFromSuperview]; CGRect frame = self.searchDisplayController.searchBar.frame; frame.origin.y += self.topBar.frame.size.height; self.searchDisplayController.searchBar.frame = frame; [self.topView addSubview:self.searchDisplayController.searchBar]; [self.topView bringSubviewToFront:self.topBar]; [self.topBar setHidden:NO]; } } 

我该如何解决这个问题?

尝试在表视图控制器中设置此值:

 if ([self respondsToSelector:@selector(edgesForExtendedLayout)]){ self.edgesForExtendedLayout = UIRectEdgeNone; } 

并将视图层次结构更改为具有视图,并将tableView,搜索栏和nag栏更改为其子视图。 使表视图控制器成为视图控制器,并将其作为数据源和委托。

或者不要使用searchBarDisplayController,只需使用带有委托方法的搜索栏:

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 

我有类似的问题,我认为searchDisplayController将searchBar大小扩展为full tableHeaderView大小(我真的不知道为什么这样做)。 我在tableHeaderView中有搜索栏和一个自定义工具栏(都是44px高度)。

我的工作方式是:

 - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { self.tableView.tableHeaderView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44); } - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { self.tableView.tableHeaderView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 88); } 

所以我只是在输入搜索时将tableHeaderView设置为单个UISearchBar的大小,并在所有动画完成时将其设置回来。 这解决了我的问题。 甚至动画仍然有效(但它们不在childViewController中。不知道为什么)