self.definesPresentationContext = true: leads to black screen? - ios

let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
// searchController.definesPresentationContext = true
self.definesPresentationContext = true
When the search bar is active, with text in it, and I go to another tab and then back, the controller is black, apart from the actual search bar. Things go back to normal when I cancel and empty the search text field.
My question is basically identical to this question: UISearchController causes black screen Swift 2.0
Except that that answer does not solve my problem as you can see. What does solve it is if I change self to searchController, thus: searchController.definesPresentationContext = true. But this results in the search bar appearing in the next controller I tab to. Very confused, please help.
I am using a tableview embedded in a navigation controller and tab bar controller. viewDidDisappear is not being called when the search is active.

Not a solution per se, but a workaround, this stackeroverflow post helped:
TableView with SearchController - DEINIT not called. I am not sure if this is some kind of apple bug.
Apparently I am not supposed to use self.definesPresentationContext = true at all. This makes my search appear in all my tabs. But at least viewDidDisappear is called.
In viewDidDisappear, I can hide the search bar with searchController.searchBar.hidden = true and show the bar again in viewDidAppear.

in the AppDelegate.swift
window?.backgroundColor = UIColor.white

Related

iOS 11 navigation bar weird behaviour with search controller

General
I've been working on an application which was not touched since the release of iOS 11. Search controller in navigation bar was always the core part of it, this controller was used as a title view to always appear at the top of the screen.
Current Status
Some research lead me to the point where I could manage to replace the old navigationItem.titleView with the standard navigationItem.searchController property so iOS can handle everything related to the transition between views and so on.
.
Problem
This is the point where I got stuck because there is a huge glitch while going from "A" view controller to "B". I don't want to use large titles, the best solution would be to completely remove title and display only the search bar as the content of the navigation bar but unfortunately I couldn't get to that point.
Edit: These view's are embedded in a tab bar controller as well.
Search Controller setup code in viewDidLoad():
// Initialize search controller and set its important attributes
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = false
// Set placeholder text
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = searchBarPlaceholder
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.returnKeyType = .search
// Replace title view with search bar
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
} else {
searchController.searchBar.barStyle = .black
self.navigationItem.titleView = searchController.searchBar
}

Swift ios11 NavigationItem SearchBar won't hide completely

Having an issue with adding a search controller to a navigationItem in iOS11 / Swift 4.
Basically everything works as expected for the most part, pull down will reveal the search bar and searching works fine. However when trying to hide the search bar by scroll back up... the bar won't hide completely and remains as a thin strip (see below).
I have declared my search controller as follows:
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.scopeButtonTitles = ["All", "Samples"]
searchController.searchBar.delegate = self
navigationItem.searchController = searchController
Before (scroll down to reveal search bar):
After (scroll up to hide):
As discussed in this Apple forums thread.
When the value of this hidesSearchBarWhenScrolling is true, the search bar is visible only when the scroll position equals the top of your content view, that's in case you are using a UIViewController, which view property is a normal UIView (It is not a subclass of UIScrollView).
Instead, try to use UITableViewController or ScrollView, it should work as expected
In my case it was happening only when there was a small num of items in the table.
I came to a non-technical, but rather a logical solution to show the search bar only when there are >10 items in the list. There is no need to have search when you have only a few items anyway.
Try to add this
self.navigationItem.hidesSearchBarWhenScrolling = true

First row hidden when table view appears while search controller is active

I'm using UISearchController with a UITableViewController. When I have a search in progress, and I select a row in the table to go to the detail view, and then go back to the table view, the first row of the table is hidden behind the search bar.
I can scroll the first row back into view, but then it just pops back behind the search bar when I let go of the scroll. So frustrating!
In viewDidLoad I have:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
mainTable.tableHeaderView = searchController.searchBar
Removing definesPresentationContext = true solves the problem, but I need that to prevent the search bar from showing up if the user segues to other view controllers.
I've spent hours searching for a solution and trying different ways to fix the problem.
Is there any way to set the first row of the table so it is below the search bar when the table view re-appears?
I tried setting definesPresentationContext = true in prepareForSegue, but that did not work.
I found a way to solve this problem.
Do not set definesPresentationContext = true
In prepare(for:sender:), add the following. This keeps the search active, but prevents the search bar and keyboard from appearing on the view controller being segued to.
searchController.searchBar.isHidden = true
searchController.searchBar.endEditing(true)
In viewWillAppear, add the following. This makes the search bar visible when the view controller with the search controller comes to the top of the navigation stack. The first row of the table is positioned properly below the search bar.
searchController.searchBar.isHidden = false
In willMove(toParentViewController:), add the following. This cancels any active search, preventing the search bar from appearing on the view controller coming to the top of the navigation stack.
if parent == nil {
searchController.isActive = false
}
This is working for me:
self.extendedLayoutIncludesOpaqueBars = true

UISearchController and UISearchBar are not animating properly and are changing the Status Bar

I'm working in a fresh project with Xcode 7.2, built against iOS 9.2, and testing on an iPhone 6. This project is less than a week old with a minimal framework primarily defined in a storyboard.
The storyboard has a float of UINavigationController > UITabBarController > UITableViewController, which is the one with the search code, defined below.
In my Info.plist, I have Status bar style set to UIStatusBarStyleLightContent, Status bar is initially hidden set to YES, and View controller-based status bar appearance set to No.
In the storyboard, I do not have a UISearchBar or anything else on the UITableViewController other than the prototype UITableViewCell.
Nowhere in my simple app do I override preferredStatusBarStyle and on the storyboard I have made sure all the Status Bar values are set to Light Content for every controller, just to be safe.
The UITableViewController has the following code in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
searchResultsController = MovieSearchResultsController()
searchResultsController.tableView.delegate = self
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = true
searchController.hidesNavigationBarDuringPresentation = true
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.searchBar.tintColor = UIColor.whiteColor()
searchController.searchBar.sizeToFit()
self.tableView.delegate = self
self.tableView.tableHeaderView = searchController.searchBar
self.definesPresentationContext = true
}
When the UISearchBar is selected, the entire view shifts upwards, covering the UINavigationBar. This is great, except the status bar changes to black text and the UISearchBar disappears.
I'm pulling my hair out and have been pouring over this all day. I've read so many answers on Stack and nothing seems to work.
When I run Apple's UISearchController demo, it works exactly as intended. I've tried to replicate this exactly and I'm having no luck. I've also tried picking apart the demo and inserting my own code and it seems to work fine, short of completely redoing the project in this container, which seems highly unnecessary.
For the time being, I've just set searchController.hidesNavigationBarDuringPresentation to false and removed self.definesPresentationContext.
My Goal:
The status bar will remain with a white text color and the background color of my UINavigationBar.
The UINavigationBar will hide as the UISearchBar animates, shifting the view up.
The animation will flow smoothly, just like in the Apple demo. No jumpiness or strange delay.

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
}

Resources