UINavigationController + UITableView + UISearchBar

2019-04-16 19:05发布

问题:

I have an annoying problem. I have an UINavigationController with an UITableView in the Master (Left) pane of my UISplitViewController. When I do normal operations, things push on on to the navigation controller fine.

alt text http://files.droplr.com/files/8851942/WDO5y.working.jpg

However, when I do a search and push things on, it's like it doesn't account for the space the navigation bar needs. It pushed the new controller on at the very top, then puts the navbar on, overlapping the content!

alt text http://files.droplr.com/files/8851942/WDRem.search.jpg

I should add it works fine when doing it in portrait (in the popup menu) and on the iphone. Is this an UISplitViewController bug?

回答1:

OK, I've got your answer- [I also posted this answer to the linked question]-

Here are the steps that worked for me [containing some pseudocode]:

1) Since you are searching you are most certainly already overriding ShouldReloadForSearch- So just add piece of code that saves/stores the forSeachString.

2) This one may vary quite a bit depending on your app- but the gist for this step is that you need to clear out the stored search string [set to null] if no search results are loaded. I did this in my UITableViewSource. I have 2 constructors- one for the full dataset and another on for the filtered [search results] dataset. So I just set it to null in my full dataset constructor. Yeah, you could do it differently no doubt but that's how I did it.

3) Next, do as the original "answer" states and call SetActive - [self.searchDisplayController setActive:NO];. They advocated doing it in override of ViewDidAppear. I didn't do that... I did this immediately after I pushed the next UIViewController in my RowSelected override.

4) Last, within your UIViewController that has the UISearchDisplayController attached- check for your saved search string and if it exists then do the following: 1) call SetActive again BUT this time with a true parameter ie setActive:YES... then 2) set the searchBar text to your saved search string value. I did this in the ViewWillAppear override.

Long story short... this worked for me AND it maintains the search results. Setting the searchBar text in step 4 is the key... this effectively gets the filter to persist.

Hope this helps someone



回答2:

I see you asked this over a month ago, don't know if you ever found a solution for it but I have been running into the same issue and searching Google keeps bringing me to this thread. I haven't tested if it happens this way on the device as I am still waiting on mine to be shipped, but it did happen like this in the simulator. I have a root view that is a UITableViewController subclass with a search bar, pushing anything onto the navigation stack will result in the view either on top or behind the navigation bar, or somehow correct position but the navigation bar being untouchable.

It finally dawned on me to set the search display controller to not be active, [self.searchDisplayController setActive:NO];, when the view is disappearing by either overwriting the viewDidDisappear or the viewWillDisappear. Now everything seems to get the right frame/bounds, but you lose the search display when popping back. If you wanted to I guess you could save the state of the search display when leaving then return it when coming back. If you found a solution another solution let me know.