UISearchController with UIBarPositionTopAttached throws the UISearchBar overs; Is it impossible to have a standard UISearchController with a UITableView?

I am trying to create a similar experience for contact apps in iOS 8. The main components of this are:

  • Hold the search bar below the navigation bar
  • When the search bar is displayed at the top of the view (standard functionality).

This, however, is easier said than done. Some time later, using tableHeaderView (which did not allow interaction before table presentation and was difficult to position viewDidLayoutSubviews ), I decided to include a UITableView within the UIViewController , so I could add a UISearchBar as a subtitle. This worked pretty well and allowed it to interact with the search bar in all scroll positions, and the inserts were not difficult to count.

But the search bar turns off below the status bar when it is activated. It seems like a direct problem - even if I did not try it with the same implementation in the UITableViewController , So I tried to make sure that all my properties are set up for alignment, in every possible controller.

 self.edgesForExtendedLayout = UIRectEdgeNone; self.extendedLayoutIncludesOpaqueBars = YES; self.automaticallyAdjustsScrollViewInsets = NO; 

I tried every possible combination of them: viewWillAppear, as well as viewDidLoad, as well as translucent navigation bars and different start frames for the table and the search bar. Bad luck. So, I tried to adjust the framework or restrictions, perhaps using topLayoutGuide or just 0. Unfortunately, adjusting the frame in any of the UISearchControllerDelegate methods did not actually change its position, and adding restrictions immediately worked when the active animation started (due to that super.top did not exist in the presentation hierarchy at that time, removing constraints in willPresent did nothing).

After the battle is still a bit, I tried to implement positionForBar as a delegate of UISearchBar :

 - (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar { return UIBarPositionTopAttached; } 

She seemed to adjust the height, but the search bar goes to the top of the view. When activated, the search bar appears directly above the visible area. This is even worse than clipping behind or below the UIStatusBar . I also tried just to hide the status bar when the search controller becomes active, but the conditional implementation of prefersStatusBarHidden does not work at all (returning YES works fine without the UISearchController ), but when it is active, it shows the status bar again or it appears below it). I assume this is because the UISearchController refuses to obey any standards or rules, as it is now painfully clear.

debug hierarchyinactive stateactive state

I have been trying to figure this out for a few days, and I cannot come up with a solution other than completely redefining the UISearchController class / animation. Please, help!

+7
ios objective-c uitableview uiviewcontroller uisearchcontroller
source share
4 answers

For the behavior you described, this works fine for me 😊

 var resultSearchController = UISearchController() 

And in your viewDidLoad function, the following:

 //Search Bar self.resultSearchController = ({ let controller = UISearchController(searchResultsController: nil) controller.searchResultsUpdater = self controller.searchBar.sizeToFit() controller.dimsBackgroundDuringPresentation = false self.tableView.tableHeaderView = controller.searchBar self.definesPresentationContext = true return controller })() 
0
source share

What worked for me is to subclass UISearchController and override prefersStatusBarHidden in the subclass.

Also make sure that the status bar on the control panel displays YES

And in your viewcontroller containing the tableview / search panel:

 self.definesPresentationContext = YES; 
0
source share

I have the same problem. I add a UISearchController to the UIViewController without using tableView.header. And UISearchBar.superview.frame.origin.y is -44. So you cannot see the searchBar.

Method 1. You can change the UISearchBarWrapperView frame in didPresentSearchController , a delegate of UISearchControllerDelegate . But you will see an animation of this process.

Method 2: e.g. JBlake. Set self.definesPresentationContext = YES; after the UISearchController init . He works!

0
source share

I realized that if you need a search bar under the navigation bar, as indicated in # 1, its very simple to set a boolean (solution for # 1) searchController.hidesNavigationBarDuringPresentation = true

Unfortunately, I could not fix the problem with the search bar off-screen, while the navigation bar was hidden.

0
source share

All Articles