UISearchDisplayController’s UISearchBar has Wrong Orientation
iPhone developers have you ever seen your UISearchDisplayController’s UISearchBar acting like it’s in Landscape like below? I came across this in working on the latest update for our Leonard Maltin Movie Guide iPhone app and only after I would return to the view after dismissing a modal view that was rotated to Landscape.

In this case, the UISearchDisplayController view shouldn’t ever rotate. So after trying to figure out what I was doing wrong I chalked it up to a problem in UIKit and thusly started looking for a “fix.” After a few hours I finally found a way to make it work as expected (see below) and figured I’d share that information.

I found a few lines of code that can fix it, and are included in my Bug example Sample Code in the MainViewController class. It’s wrapped in a condition check on a kFixTabBarView constant so the error can be easily reproduced without my fix getting in the way.
The gist of the fix that you want to override viewWillDisplay:animated: in your UISearchDisplayController subclass. During that you’ll want to toggle setActive:animated: off and then on again to get the UISearchBar to redraw properly if the view exited with the search active. Below is some basic example code:
[self.searchDisplayController setActive:NO animated:NO]; [self.searchDisplayController setActive:YES animated:NO];
By doing this you’ll reset the state the UISearchBar was in, so you’ll have to reassign the search text and selected scope button to it’s previous state along with the table’s contentOffset. I also block out the call to searchDisplayController:shouldReloadTableForSearchString in this case to prevent the search from re-executing as the results already exist for that search. Again all code for this can be found in my Bug example Sample Code (it was too long for this blog).
I opened a bug report radar with Apple on this issue already, you can see the details of the radar over at Open Radar, Apple employees can use the link: rdar://7478456. I hope this finds the way to someone else with this problem, as I know it resulted in many hairs pulled out of my head.



