Search code examples
objective-cxcodeipaduisplitviewcontrolleruisearchbar

UISearchBar does not get focus on iPad split view


this is my first question on Stackoverflow. I have used SO extensively while coding an universal iOS App.

I've hit a dead end with an issue about a UISearchBar: I have an universal app, with a tableview and a search bar. On the iPhone, the tableview is the main view controller, on the iPad, it's the masterViewController of a splitViewController. There are different XIBs for the 2 devices, but they share the same implementation code. The searchbar works fine, except when I first scroll down the tableview.

When I activate the searchbar with a button on the iPhone with

[self.searchBar becomeFirstResponder];

the table scrolls all the way up, the searchbar comes in view, the keyboard appears and I can start typing. Fine.

On the iPad though, where the tableview is part of the UISplitView, when I call becomeFirstResponder, the tableview does NOT scroll, the search bar does not come in view and I have no control over the search bar (nor can I click the cancel button since I don't see it). The SearchDisplayController is activated though because the tableview gets dimmed and the navigationbar moves out of view.

Can you give me any clue where to look ? I have made sure that everything is wired up in the xib ...

Cheers, Bob


Solution

  • I found a workaround that works for now, but it's a little strange that the workaround is only needed on the iPad ... Just move the origin of the tableView's bounds to 0,0 before calling becomeFirstResponder on the searchbar. On the iPhone, this happens "automatically".

        CGRect newBounds = self.mainTableView.bounds;
        newBounds.origin.y = 0;
        self.mainTableView.bounds = newBounds;