UISearchDisplayController lightbox doesn't cover tableview - ios

I have gone through all the documentation I can find, but I just can't figure out how to solve this issue. When I click on the search text box, the lightbox comes up, but does not cover the whole screen (looks like it is off by ~64dp). Here are a couple of screenshots.
I have my search stuff inside of a tableViewController
Before:
After:
Has anyone had experience fixing this issue?
Here is how I am setting everything up:
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.barTintColor = [UIColor whiteColor];
self.searchBar.delegate = self;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.displaysSearchBarInNavigationBar = YES;

Turns out I had a self.edgesForExtendedLayout = UIRectEdgeNone; in the viewDidLoad, and that messed EVERYTHING up...

Related

UISearchController in UITabBarController is not responding to touch

I have a tabbarcontroller, which is inside of a uinavigationcontroller, and I am trying to put a searchbar in the tableviewheader on one of the tabs. I am able to successfully use the code below on any screen on the navigationcontroller but the search will not respond to touch when the view is on the tabbarcontroller. I can successfully set it to active in code but I need touch to work as well.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = #[#"SEARCH"];
[self.searchController.searchBar setPlaceholder:#"SEARCH"];
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
self.searchController.searchBar.backgroundColor = [UIColor colorWithRed:0.843 green:0.843 blue:0.843 alpha:1];
self.searchController.searchBar.tintColor = [UIColor blackColor];
self.myTableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
I had a similar issue and found that the line self.definesPresentationContext = YES; was the root cause of the problem (for more information: UISearchController and definesPresentationContext).
Make sure that the class setting self.definesPresentationContext = YES; is also a parent view controller of the search bar. If you're initializing the UISearchController using the code you provided and then assigning the search bar to a view outside the hierarchy this might be causing the problem.

Why is my Search Bar within the friendsviewcontroller not displaying?

I have two ways of searching for friends within my app. One of them is a snapchat similar interface where the added friends are shown and possibility of adding a friend is shown with a + sign. I tried adding a search bar somewhere within the area programmatically but got no results of nothing displaying. The code was implemented on my FriendsViewController that I am using and this is the current code I have:
-(void)initialiseSearchBar
{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
searchResultsController.tableView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
[self.tableView setContentOffset:CGPointMake(0, 0)];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.searchResults = [NSMutableArray array];
}
Any idea what I have done wrong or should fix? Also I am planning on releasing with iOS 7 compatibility and up all the way until 8.1, I have worries that the terms might be different or there is something depreciated that I do not know of.
You've allocated it but you haven't added it to the view itself unless it was made in storyboard (which you clearly state you didn't do) see my answer on another question... you have to add it as a subview somehow:
Adding A Search Bar

How to add UISearchBar to UIView?

I want to add a UISearchBar to an UIView instead of an UIViewController, the problem is that the init method of UISearchDisplayController needs an UIViewController as contentsController:.
I can see the UISearchBar correctly displayed in the view but if I click in it, the bar disappears. Here is my code I'm currently using:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
// this was my approach, but this DOES NOT work
UIViewController *results = [[UIViewController alloc] init];
results.view.userInteractionEnabled = NO;
[self insertSubview:results.view aboveSubview:self.menuList];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:results];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = self.searchBar;
Before:
After clicking into search bar:
In swift,
I use hidesNavigationBarDuringPresentation to solve this problem.
This problem seems like after the search bar is clicked, it will automatically hide the bar. And it will be fine after you click outside the view.
I add this line:
self.searchController?.hidesNavigationBarDuringPresentation = false;
Hope it works

Avoid UISearchBar resizing

I have a UITableView as a UIView subview. This table view has a UISearchBar, which is working correctly, but it's changing it's position when it becomes first responder:
Code:
self.searchBar = [UISearchBar new];
self.searchBar.delegate = self;
self.searchBar.placeholder = #"Buscar";
self.searchBar.text = #"";
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = self.searchBar;
Is there anyway to avoid this change? I'd like it to remain at the same place when selected.
Thanks!
Here's the solution: http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/
UISearchDisplayController is pretty bad on iOS 7.

UISearchDisplayController strange animation and offset

I have created a UITableViewController and added a UISearchDisplayController programmatically. There's no storyboard or nib file as I am doing everything programmatically. Everything seems to work perfectly except when I click on the search bar, it animates weirdly and there's some offset left to the bottom of the search bar.
Here's a video of the behaviour.
And here's the code of Table VC:
// Initialize Search Bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
self.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
// Hide the search bar
[self.tableView setContentOffset:CGPointMake(0, 44)];
I also tried [self setEdgesForExtendedLayout:UIRectEdgeNone]; on the UITableViewController's implementation but it created another issue, although it seems to fix the animation by some extent leaving some gap at the top and the same gap at the bottom of search bar.
Edit: One more thing to noteā€”it can be seen in the video that the result table view is properly aligned with the search bar. So is there any problem with the implementation of main table view? They are same but the former is aligned properly so I am guessing there's a problem with the table view. What do you think?
iOS7 makes programmatically adding these elements unnecessarily difficult. After much testing in my own app, here's how I did it inside of my UITableViewController.
// _searchBar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
self.searchBar.delegate = self;
// _searchController
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:[self searchBar] contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsTableView.frame = [self.tableView frame];
// _tableView
self.tableView.tableHeaderView = [self.searchController searchBar];
Don't worry about hiding the searchBar, as the OS will reset everything to its original state when you hit cancel.

Resources