Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar

前端 未结 9 955
难免孤独
难免孤独 2020-12-14 03:13

I am building an iOS 7-only app. I am trying to set a UISearchDisplayController into the navigation bar.

I have it set up like this: In the storyboard, I added a \"

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 03:35

    Unfortunately none of the above solutions worked for me, I'm using a UITableViewController.

    This link helped:

    http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/

    I put the code below for convenience:

    static UIView *PSPDFViewWithSuffix(UIView *view, NSString *classNameSuffix) {
        if (!view || classNameSuffix.length == 0) return nil;
    
        UIView *theView = nil;
        for (__unsafe_unretained UIView *subview in view.subviews) {
            if ([NSStringFromClass(subview.class) hasSuffix:classNameSuffix]) {
                return subview;
            }else {
                if ((theView = PSPDFViewWithSuffix(subview, classNameSuffix))) break;
            }
        }
        return theView;
    }
    
    - (void)correctSearchDisplayFrames {
        // Update search bar frame.
        CGRect superviewFrame = self.searchDisplayController.searchBar.superview.frame;
        superviewFrame.origin.y = 0.f;
        self.searchDisplayController.searchBar.superview.frame = superviewFrame;
    
        // Strech dimming view.
        UIView *dimmingView = PSPDFViewWithSuffix(self.view, @"DimmingView");
        if (dimmingView) {
            CGRect dimmingFrame = dimmingView.superview.frame;
            dimmingFrame.origin.y = self.searchDisplayController.searchBar.frame.size.height;
            dimmingFrame.size.height = self.view.frame.size.height - dimmingFrame.origin.y;
            dimmingView.superview.frame = dimmingFrame;
        }
    }
    
    - (void)setAllViewsExceptSearchHidden:(BOOL)hidden animated:(BOOL)animated {
        [UIView animateWithDuration:animated ? 0.25f : 0.f animations:^{
            for (UIView *view in self.tableView.subviews) {
                if (view != self.searchDisplayController.searchResultsTableView &&
                    view != self.searchDisplayController.searchBar) {
                    view.alpha = hidden ? 0.f : 1.f;
                }
            }
        }];
    }
    
    // This fixes UISearchBarController on iOS 7. rdar://14800556
    - (void)correctFramesForSearchDisplayControllerBeginSearch:(BOOL)beginSearch {
        if (PSPDFIsUIKitFlatMode()) {
            [self.navigationController setNavigationBarHidden:beginSearch animated:YES];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self correctSearchDisplayFrames];
            });
            [self setAllViewsExceptSearchHidden:beginSearch animated:YES];
            [UIView animateWithDuration:0.25f animations:^{
                self.searchDisplayController.searchResultsTableView.alpha = beginSearch ? 1.f : 0.f;
            }];
        }
    }
    
    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
        [self correctFramesForSearchDisplayControllerBeginSearch:YES];
    }
    
    - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
        [self correctSearchDisplayFrames];
    }
    
    - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
        [self correctFramesForSearchDisplayControllerBeginSearch:NO];
    }
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
        // HACK: iOS 7 requires a cruel workaround to show the search table view.
        if (PSPDFIsUIKitFlatMode()) {
            controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(self.searchDisplayController.searchBar.frame.size.height, 0.f, 0.f, 0.f);
        }
    }
    

提交回复
热议问题