zarghol
zarghol

Reputation: 498

UISearchDisplayController doesn't display the result tableview on iPad

I want to use a UISearchDisplayController in a custom UIViewController with a MKMapView.

I display the UISearchBar in the Naviguation Bar.

All works on iPhone but on the iPad, the TableView for results isn't shown, even in popover... I just want to display this tableview...

(all delegates are well-initialized)

By debugging, I know the TableView exists (there is an address into the searchDisplayController)

self.searchDisplayController.searchResultsTableView;

EDIT :

I setup all in Interface Builder, except the insert of the searchBar in the navigation bar. I just did it programmatically :

self.searchDisplayController.displaysSearchBarInNavigationBar = YES;

I don't understand, why in the iPhone, the tableView is shown and not in the iPad !

EDIT 2 :

If I remove the insertion into the navigation bar (the last line of code quoted above), the tableView is shown, but in fullscreen instead of in a popover.

FINAL EDIT :

Without solution, I tried it differently : I embedded my view in a SplitView, with the Research in the master view and the MapView in the detailView. It works, but it looks differently than expected..

AND THE ANSWER IS :

With the swift and iOS 8, I rebuilt my app and I found a solution to do this work well.

    if let tableView = self.searchDisplayController?.searchResultsTableView {
        tableView.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height)
        self.view.addSubview(self.blurEffectView!)
    }

Upvotes: 2

Views: 1836

Answers (3)

Cedrick
Cedrick

Reputation: 566

This fixed the problem for me.

UISearchBar *searchBar = [UISearchBar new];
searchBar.delegate = self;
[searchBar sizeToFit];

self.navigationItem.titleView = searchBar;

And then don't displaysSearchBarInNavigationBar to YES

Upvotes: -1

Nagarajan Sathish
Nagarajan Sathish

Reputation: 137

@Jeremy thanks for your answer. I'm new to iOS and your answer helped me to get rid of this issue. Here is an update which might be helpful for others.

Include this piece of code in ViewDidLoad to hold the copy of ur original view

originalView = self.view;

Add the following two delegate methods for the searchDisplayController will solve your problem. If your placing the searchBar in NavigationBar then use

UIEdgeInsetsMake(70, 0, 0, 0)

else use UIEdgeInsetsZero

 - (void)searchDisplayController:(UISearchDisplayController *)controller  didShowSearchResultsTableView:(UITableView *)tableView
    {
        if (OSVersionIsAtLeastiOS7() && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)) { 
            if (([originalView isEqual:self.view])) {
                self.view = controller.searchResultsTableView;
                controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(70, 0, 0, 0);
                [self.searchDisplayController.searchBar becomeFirstResponder]; // This will retain the keyboard after keypress
            }
        }
    }

To show the original view anytime use self.view = originalView; Here in this delegate method I used, if the search text is empty then, to show the original view instead of black screen.

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    if ([searchString isEqualToString:@""]) {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            self.view = originalView;
        }
    }
    else{
    [self filterContentForSearch:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    }
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    originalView.alpha = 0.5f;
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    originalView.alpha = 1.0f;
}

Upvotes: 2

Jeremy Hicks
Jeremy Hicks

Reputation: 3740

I'm having the same problem. A partial solution is to implement

- (void)searchDisplayController:(UISearchDisplayController *)controller  didShowSearchResultsTableView:(UITableView *)tableView
{
    if (OSVersionIsAtLeastiOS7() && IPAD) { // These are custom methods I have in my .pch file
        self.view = controller.searchResultsTableView;
        controller.searchResultsTableView.contentInset = UIEdgeInsetsZero;
    }
}

Found this solution here: http://www.objc.io/issue-5/iOS7-hidden-gems-and-workarounds.html

Unfortunately, there is is still a slight problem - when the user starts to type in the search box, the keyboard gets dismissed (for me) after the first character is typed.

Upvotes: 1

Related Questions