UISearchController search bar shifting down when presented - ios

Issue: Searchbar shifts down when presented. Before presented, the bar is right below the navigation bar.
More Info:
The navigation bar is just a UINavigationBar that's manually added to a UIViewController through storyboard
Most importantly, the UIViewController uses UIPresentationController to create that effect where the presented VC is slightly offset from the top and the presenting VC is scaled down and "behind" the presented VC. The shift does not happen if I don't use a UIPresentationController.
searchController.hidesNavigationBarDuringPresentation = NO
Any Ideas?

self.definesPresentationContext = YES;
or
self.edgesForExtendedLayout = UIRectEdgeNone;
On a side not it is probably better to add the searchbar to the header of the tableview.

if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}
if you set searchBar to tableHeaderView under iOS 11.0 will cause this question, so you can do it like this. everything will be ok!

Related

Weired animation of tableview and searchbar of UISearchController embed in navigationbar

I've embed UISearchController into the navigationbar and my view controller has the table view. When I click on searchbar and keyboard appears tableview and searchbar does not animate smoothly. It seems like searchbar is overlaping navigationbar.
Here is the code,
Declare searchcontroller as a variable like,
var resultSearchController = UISearchController()
and defination in viewDidLoad is like,
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.obscuresBackgroundDuringPresentation = false;
self.navigationItem.searchController = controller
return controller
})()
Animation issue is like below gif(watch till end to see slow animation)
I want same duration for animation for tableview, navigation bar, and searchbar.
Any help will be appreciated.
Don't set UITableView top anchor to be pinned to safeArea but to superview.
Important extra note to the accepted answer:
If you use a UIViewController with subviews, including a UITableView, the UITableView has to be the first subview (i.e.: at index 0).

Dont animate navigationBar transition when push ViewController with SearchController and before in navigation were too a VC with SearchController

i have problem with animation of navigation bar when pushing other view controller also with UISearchController to navigation controller. If i push ViewController without SearchController everything is OK. The problem can be seen when on the screen with UITableView scroll down for display UISearchController in navigation bar then push UIViewController too with SearchController to UINavigationController, but on place of UISearchController is only some gray view and the animation of transformation the navigation bar isnt fluent. The gray view disappers after one sec (see image).
This is how it looks like:
This is how I add the UISearchController to navigationBar in viewDidLoad(_:):
private let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
In viewDidLoad(_:) i call only this:
private func setupSearch() {
searchController.searchBar.placeholder = "Suche"
searchController.searchBar.setValue(abbrechen, forKey: cancelButtonText)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = true
}
Do you know where could be problem? Thx for your time.

SearchBar goes out of UINavigationItem frame when it is active

I placed searchController's searchBar in navigationItem as the titleView as follows:
self.navigationItem.titleView = self.searchController.searchBar;
Although it fits perfectly when it is not selected (cancel item is not shown), if it becomes first responder and cancel item appears next to the searchBar, it slides under of the navigationItem slightly. You can see the result below:
How can I fix this? Is there a workaround?
You should use self.navigationItem.searchController instead of self.navigationItem.titleView.
self.navigationItem.searchController = self.searchController;
Learn more here https://developer.apple.com/documentation/uikit/uinavigationitem/2897305-searchcontroller
Use below code
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
// Fallback on earlier versions
navigationItem.titleView = searchController?.searchBar
}

Add UISearchBar to UINavigationBar with UIBarButtonItem

In my app, I used to have a search bar in the header view of my table view. However, I have added a search bar button item, and when a user taps it, I want a search bar to animate across the navigation bar, and the user should be able to search. This is kind of like the search bar in the Twitter iOS app. here is the code for my UISearchController:
self.searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = true
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
//self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
controller.searchBar.returnKeyType = .Search
controller.searchBar.delegate = self
return controller
})()
Here is how my UINavigationBar looks like right now:
How would I go about doing this?
You're almost there. You need to do four things:
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = searchController.searchBar
hide all the current UIBarButtonItems you have in the navigation bar
searchController.searchBar.becomesFirstResponder()
That is, you setup your UISearchController so it doesn't hide the navigation bar, as the UISearchBar is gonna be displayed there.
Then, when your IBAction runs, you do something like:
navigationItem.hidesBackButton = true
navigationItem.titleView = searchController.searchBar
navigationItem.rightBarButtonItem = nil
searchController.searchBar.becomeFirstResponder()
That guarantees the UISearchBar will use the entire UINavigationBar when it becomes the first responder. You also get the UISearchBar animation, which does a good job in hiding the unanimated removal of the UIBarButtonItems.
You will need to implement the UISearchBarDelegate (or if you prefer UISearchControllerDelegate) to restore the original navigation bar state once the user is done with the search.
One very important detail is that in order for your UISearchBar to be editable in the titleView, no other view controller can be set with definesPresentationContext = true at the time yours is pushed.

UIViewController under UINavigationBar?

I have a UINavigationController and a UIViewController as its root view controller.
let rootVC = Page1ViewController() // extends UIViewController
let nav = UINavigationController(rootViewController: rootVC)
presentViewController(nav, animated: true) { () -> Void in
}
The problem is that the content of the rootVC appears under the navigation bar. I tried:
nav.navigationBar.translucent = false
this worked but I want the navigation bar to be transparent and I want the content of the rootVC not appear behind the navigation bar.
I also tried:
nav.edgesForExtendedLayout = UIRectEdge.None
but this does not change anything.
How can I get a transparent navigation bar where the content scrolls under it when scrolling but when loading the content should not appear under the navigation bar?
Remove this line:
nav.edgesForExtendedLayout = UIRectEdge.None
And add this line in your viewDidLoad from Page1ViewController
self.edgesForExtendedLayout = UIRectEdge.None
Hope it helps. Let me know if it doesn´t work and I will help you.
To address your issue of a blackbox now appearing underneath your UINavigationBar you need to set the presentation context properly. Try setting definesPresentationContext on your UINavigationController to NO. This will then use the UIWindow or parent of your UINavigationController as the presentation context.

Resources