带有displaysSearchBarInNavigationBar的UISearchDisplayController将结果视图向下推导navigationBar.translucent = false

我正在使用UISearchDisplayController与新的ios 7functiondisplaysSearchBarInNavigationBar和不透明的导航栏。 search显示控制器似乎不正确地定位它的视图。

我试图插入委托方法和重新定位,但我不能得到正确的初始位置,也不能旋转。 另外,这似乎是一个马虎的解决scheme。

在搜索栏中没有文字

与在搜索栏的文本

只是启用“在不透明的酒吧”在视图控制器的故事板或如果你想代码。然后添加下面的lines.Your好:)

 self.edgesForExtendedLayout = UIRectEdgeAll; self.extendedLayoutIncludesOpaqueBars = YES; 

我有同样的问题,我已经这样修复:

  1. 子类UISearchDisplayController在iOS 6和7的NavigationBar中有UISearchBar。我覆盖了:

     - (void)setActive:(BOOL)可见animation:(BOOL)animation
     {
        如果(SYSTEM_VERSION_LESS_THAN(@“7”)){
             if(self.active == visible)return;

             [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];

            如果(可见){
                 [self.searchBar成为FirstResponder];
             } else {
                 [self.searchBar resignFirstResponder];
             }
         } else {
             self.searchContentsController.view.frame = CGRectMake(0,0,kCurrentScreenWidth,kCurrentScreenHeight);
             [super setActive:visible animated:animated];
         }
     }

2.在UISearchDisplayDelegate我已经添加了这个:


     - (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
         // iOS7黑客
         if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@“7”)){
             controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(0.f,0.f,0.f,0.f);
         }

     }

     - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString :( NSString *)searchString
     {
         //  -  iOS 7 Hack

         if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@“7”)){
             controller.searchResultsTableView.frame = CGRectMake(0,64,kCurrentScreenWidth,kCurrentScreenHeight-64);
             [controller.searchContentsController.view setNeedsLayout];
         }
     }

我有同样的问题,并在search答案后,我决定通过视图层次结构。 searchBar的顶视图,也是朦胧的视图,它的起源有64个,高度为504,没有填满整个屏幕。 不知道为什么这样。 尽pipe如此,我最终将y设置为0,并且它是屏幕高度的高度。 之后,我将其y设置回原始值,否则您的内容表视图将会失真。 这不是最好的解决scheme,但总比没有好。 希望这可以帮助你。

 - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { self.savedSearchTerm = nil; UIView *dimmedView = controller.searchBar.superview; CGRect frame = dimmedView.frame; frame.origin.y = 64; dimmedView.frame = frame; [self.tableView reloadData]; } - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { UIView *dimmedView = controller.searchBar.superview; CGRect frame = dimmedView.frame; frame.origin.y = 0; frame.size.height = 568; dimmedView.frame = frame; } 

我已经无休止地在网上search这个问题的解决scheme,但没有任何build议在我的情况下工作。 重置searchResultsTable的框架不起作用,因为它的origin.y已经在0了。改变了contentInsettypes的工作,但没有修复暗淡的叠加视图,并导致在底部(和酒吧)表的滚动视图的问题。 我终于做了一个更好的工作黑客,虽然它不是完全理想的,因为视图的框架变化是显而易见的,但至less在这之后的定位是正确的。

使用Reveal.app,我能够深入到UISearchDisplayController的视图层次结构中,弄清楚发生了什么,这是我的结果:

 UISearchDisplayControllerContainerView - UIView (0,0,320,504) - UISearchResultsTableView - UIView (0,20,320,44) - UIView (0,64,320,440) - _UISearchDisplayControllerDimmingView 

我以编程方式执行所有操作,所以不确定NIB的解决方法。 以下是我的viewDidLoad方法中如何设置我的UISearchBar和UISearchDisplayController的基础知识:

 UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 0)]; searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; searchBar.delegate = self; [searchBar sizeToFit]; self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; self.searchController.delegate = self; self.searchController.searchResultsDataSource = self.searchDataSource; self.searchController.searchResultsDelegate = self; if ([self.searchController respondsToSelector:@selector(displaysSearchBarInNavigationBar)]) { self.searchController.displaysSearchBarInNavigationBar = YES; } 

而我的破解在这种情况下工作:

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fixSearchControllerPositionOnKeyboardAppear) name:UIKeyboardWillShowNotification object:nil]; if (self.searchController.isActive) { // the following is needed if you are return to this controller after dismissing the child controller displayed after selecting one of the search results [self performSelector:@selector(fixSearchControllerPositionForiOS7) withObject:nil afterDelay:0]; } } } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; } - (void)fixSearchControllerPositionForiOS7 { UIView *view = self.searchController.searchResultsTableView.superview; // only perform hack if the searchResultsTableView has been added to the view hierarchy if (view) { // The searchDisplayController's container view is already at 0,0, but the table view if shifted down 64px due to // bugs with the subviews in iOS 7, so shift the container back up by that negative offset. // This also fixes the position of the dimmed overlay view that appears before results are returned. CGFloat yOffset = 64.0; CGRect viewFrame = view.frame; if (CGRectGetMinY(viewFrame) == 0) { viewFrame.origin.y = -yOffset; viewFrame.size.height += yOffset; [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ view.frame = viewFrame; } completion:nil]; } // we also need to adjust dimmed overlay view, so iterate through the search view controller's container // view and make sure all subviews have their vertical origin set to 0 UIView *searchContainerView = view.superview; for (NSInteger i = 0; i < [searchContainerView.subviews count]; i++) { UIView *subview = searchContainerView.subviews[i]; if (CGRectGetMinY(subview.frame) > 0) { CGRect subviewFrame = subview.frame; CGFloat offset = CGRectGetMinY(subviewFrame); subviewFrame.origin.y = 0; if (offset == 20.0) { // this subview is partially responsible for the table offset and overlays the top table rows, so set it's height to 0 subviewFrame.size.height = 0; } else { // this subview is the dimmed overlay view, so increase it's height by it's original origin.y so it fills the view subviewFrame.size.height += offset; } subview.frame = subviewFrame; } } } } - (void)fixSearchControllerPositionOnKeyboardAppear { // call hack to reset position after a slight delay to avoid UISearchDisplayController from overriding our layout fixes [self performSelector:@selector(fixSearchControllerPositionForiOS7) withObject:nil afterDelay:0.1]; } - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { [self fixSearchControllerPositionForiOS7]; } } 

我不得不添加一个观察者,当键盘出现,因为这是导致UISearchDisplayController重新布局其子视图,以及一个短暂的延迟,以确保我的位置调整应用UISearchDisplayController做了布局的东西。

1.使用Reveal,find覆盖层,发现是_UISearchDisplayControllerDimmingView

2.查找图层,修改相应的框架,可以通过dimmingview在同一视图的searchResultsTableView子图层上find它。

 - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller { for(UIView * v in controller.searchResultsTableView.superview.subviews) { if([v isKindOfClass:[NSClassFromString(@"_UISearchDisplayControllerDimmingView") class]]) { v.frame = CGRectMake(0,20,320,400); // modify the frame NSLog(@"--------- %@",[v class]); } } 

同样,如果您需要调整searchResultsTableView框架,请在下面的代码中添加下面的代码

 - (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView { tableView.frame =CGRectMake(0, 20, 320, 480-64-44); }