UITableViewContoller in UINavigationController with UISearchController wrong after back button - ios

We have a UINavigationController with a UITableViewController. We have a UISearchController to filter the list of items in the table. When we tap a cell it navigates to the detail. But when we navigate back, its as if the UISearchBar is on top of the content... like instead of being in the table header, it is now over top of the table. How can we get it to behave as being in the table header.
func buildSearchBar() {
self.searchController.searchResultsUpdater = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
self.searchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = searchController.searchBar
}
Edit to add: we set all UINavigationBar appearance to be translucent = NO; This is the line of code that seems to break it for us. Does that sound right to anyone else?
[UINavigationBar appearance].translucent = NO;

I had a similar problem. Add the following line to the viewDidLoad method of your view controller (self in the code you posted in your question).
self.definesPresentationContext = true;
This fixed the problem for me.

Related

Navigationbar with searchbar will not show large title

I have a UISplitViewController with a UINavigationController as the masterviewcontroller. This UINavigationController has a UICollectionViewController as its rootViewController. In the UICollectionViewController, I have set the following parameters:
self.collectionView.scrollEnabled = YES;
self.collectionView.bounces = YES;
self.collectionView.alwaysBounceVertical = YES;
self.navigationController.navigationBar.prefersLargeTitles = YES;
UISearchController *sc = [[UISearchController alloc] initWithSearchResultsController:nil];
self.navigationItem.searchController = sc;
The UISearchBar does show up in the navigationBar, but when I launch the app the navbar is collapsed to the small title. It's only when I drag the view downwards that the large title and the search bar appear.
Where I expect the view to launch like this
it actually launches like this
Any ideas what could be causing this?
Make sure you set preferLargeTitles to true in your UINavigationBar; either in code or in the Storyboard file.
Put this in your viewDidLoad.
self.navigationController?.navigationBar.prefersLargeTitles = true

iOS - segmentedControl disappears after using searchBar

Through Storyboard, I have created a ViewController with a UITableView and a SegmentedControl on top. Programatially, I have added a SearchController as the TableView's header.
This is what happens when I launch the app:
It works perfectly.
But when I click on the search bar, it already messes up a bit. It's easy to fix by hiding the SegmentedControl though.
Now the problem appears when I finish my search:
The SegmentedControl disappears. Whenever I click on the searchbar again, it comes back, just like in the picture number 2, but if I cancel again, it disappears.
I have tried removing it from the view and adding it again once the user stops searching, but it changes nothing. Here is my code:
searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = searchController.searchBar;
searchController.searchBar.scopeButtonTitles = #[];
self.definesPresentationContext = YES;
[searchController.searchBar sizeToFit];
What can I change?
searchController.searchBar.showsScopeBar = YES;

UISearchController searchBar disappears on first click

I have implemented a UISearchController in a TableView, pushed by a Navigation Controller.
First my problem was that whenever I click on the SearchBar, it disappears. It works when I enter some text, but it stays completely blank. Then I managed to semi solve the issue using this code:
- (void)searchForText:(NSString*)searchText
{
[self.view addSubview:villeSearchController.searchBar];
}
Which semi-works because now, when I click on the search bar, it blanks out, but if I enter one character, it appears again, and then it stays there, no matter what. Until I cancel the search, and click on it again, in that case it blanks out.
I have made some tests and this method (searchForText) is called on the very first click, so that isn't the reason.
Does anyone know how I can solve this issue and make the searchbar appear from the very first click?
EDIT:
This is how I initialize the SearchController:
villeSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
villeSearchController.searchResultsUpdater = self;
villeSearchController.dimsBackgroundDuringPresentation = NO;
villeSearchController.searchBar.delegate = self;
villeTableView.tableHeaderView = villeSearchController.searchBar;
villeSearchController.searchBar.scopeButtonTitles = #[];
self.definesPresentationContext = YES;
[villeSearchController.searchBar sizeToFit];
This happened to me when the UISearchController was hiding the navigation bar. Setting this property fixed it:
UISearchController.hidesNavigationBarDuringPresentation = NO;
Try to check the navigationBar.translucent property - it should be YES when UISearchController will present the searchBar or else will be UI bugs.
Update from #SiavA
The better solution is use the extendedLayoutIncludesOpaqueBars property of the UIViewController. If you using the opaque navigation bar just set it in the true for controller which will be show UISearchController (not for navigationController).
E.g.
- (void)viewDidLoad {
[super viewDidLoad];
self.extendedLayoutIncludesOpaqueBars = !self.navigationController.navigationBar.translucent;
}
Place the SearchController inside a UIScrollView and it will work fine. This if you are using it in the section header or as a separate view
If you run into this problem in iOS11 (and especially if it worked pre iOS11), I had to change my UISearchController to be attached to the navigationItem rather than the tableView.
After setting parameters on my searchController, I used to do this:
tableView.tableHeaderView = searchController.searchBar
Now I have this:
navigationItem.searchController = searchController
The "translucent" fix would allow the controller to appear, but when I would try and unwind to a specific segue, I'd get a crash. Attaching the searchController to the navigationItem fixed both the display and the crash.
Setting isHidden of the navigation bar to false stopped the search bar from disappearing for me.
self.navigationController?.navigationBar.isHidden = false
Hi Guys there is a very simple solution for the issue.
-- this will solve major issues with collections of views and a parent view containing multiple viewcontrollers.
you don't need any below code if you have just remove it from your code
// searchController.definesPresentationContext = true
// self.definesPresentationContext = true
// self.extendedLayoutIncludesOpaqueBars = !(self.navigationController?.navigationBar.isTranslucent)!
I just add below code make sure you set searchController always set nil
if you are switching between viewControllers there may be some conflicts that can be cleared by setting it searchController nil in the setupsearchbar().
also, disable the searchController on viewDidDissapear(), it will solve the issue related to active search bar moving on to the next screen.
var searchController = UISearchController(searchResultsController: nil)
func setupSearchBar() {
searchController = UISearchController(searchResultsController: nil)
// adding search controller
searchController.searchResultsUpdater = self
// changing font color when user types
searchController.searchBar.searchTextField.textColor = .black
//allows select results from filtered table
searchController.searchBar.endEditing(true)
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
self.tableView.tableHeaderView = searchController.searchBar
}
override func viewDidDisappear(_ animated: Bool) {
searchController.isActive = false
}

How can I make a UISearchController start hidden?

I have added a UISearchController to my code using the following method:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = #[];
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
self.definesPresentationContext = YES;
This creates my search controller and adds it to the top of my tableView. Annoyingly it starts visible though:
I can hide it by sliding it up under the navigation bar which suggests the underlying functionality of the code is working but I can't get it to start hidden so I can slide it down.
I have tried adjusting the edge insets, I have tried setting the navigation bar to translucent, I have tried to go through the search bar tutorials online but nothing seems to be dealing with this issue.
Any help very welcome
Did you try setting the content offset of your table view?
[self.tableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height) animated:NO];
Here is for swift 4
tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: true)
From iOS 11.0 onwards you can use,
self.navigationItem.searchController = self.searchController;
The search bar will be hidden, unless you swipe down to reveal it.

UISearchController searchbar hides the first cell in tableview

I have a tableview with a search bar. The search bar is provided by a UISearchController. When I add the search bar to the header view of the table, the first row of the table gets covered by the search bar.
How do I prevent the search bar from hiding the first row?
I have this snippet in viewDidLoad:
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
It seems that you have to explicitly set the scope button titles array if you don't have scope button titles.
self.searchController.searchBar.scopeButtonTitles = [NSArray array];
Found this issue to be a layout constraints issue. Resolved by dropping all of my constraints in the view containing my searchcontroller and adding back in individually until I found the offending constraint. Using Xcode 7.1

Resources