How to fix jumping upon clicking search bar? - ios

I've tried to add a simple search controller but whenever I'm clicking on it so it becomes active, it jumps up, out of screen. Why is that even happening? I checked all the code and I'm not manipulating with constraints or anything.
I initialize my search controller through the following function (and call it in ViewDidLoad):
private func initSearchController() {
self.searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.searchBar.delegate = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.barStyle = UIBarStyle.black
controller.searchBar.barTintColor = UIColor.white
controller.searchBar.backgroundColor = UIColor.clear
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}

self.searchController.hidesNavigationBarDuringPresentation = false

Related

Why searchController nested in the tableView scrolled under the navigationBar?

I have tableViewController at the storyboard.
I programmatically added searchController to the table header.
private func setupSearchView(){
if !showSearchBar { return }
let storyboard = UIStoryboard(name: "Search", bundle: nil)
resultViewController =
storyboard.instantiateViewController(withIdentifier: "NewSearchTableViewController") as? SearchResultTableViewController
searchController = UISearchController(searchResultsController: resultViewController)
resultViewController?.tableView.delegate = self
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self // Monitor when the search button is tapped.
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = "Search here...".localized
definesPresentationContext = true
}
When I selected the cell in the table and went to the details screen - it works perfectly.
But when I pressed button back, the search bar was scrolled under the navigation bar.
Normal behaiver
Bad behavior after button back pressed

Cannot Interact with SearchBar added as HeaderView

I have added SearchController's searchbar to tableview controller's headerview but when I tap on the searchbar keyboard doesn't pop up. it feels like there is no user interaction. Following is the code used to add searchbar to uitableview.
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.userInteractionEnabled = true
dispatch_async(dispatch_get_main_queue(),{
self.tableView.tableHeaderView = controller.searchBar
controller.searchBar.bounds = (self.tableView.tableHeaderView?.bounds)!
controller.definesPresentationContext = true
self.definesPresentationContext = true
})
return controller
})()
Try to add just this 5 lines of code in your viewDidLoad and give it a try
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
self.tableView.tableHeaderView = searchController.searchBar

UITableView under UISearchBar on MasterViewController (UISplitViewController)

I have a problem when trying to add a search bar (UISearchController) on the header of the table of MasterViewController.
As you can see, the table is getting under the search bar.
Any ideas on how can I fix this?
Here is the code that I use for the MasterViewController
private var searchController : UISearchController = ( {
let controller = UISearchController(searchResultsController: nil)
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
return controller
})()
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.translucent = false
self.edgesForExtendedLayout = .None
self.extendedLayoutIncludesOpaqueBars = false
self.automaticallyAdjustsScrollViewInsets = false
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
self.tableView.tableHeaderView = searchController.searchBar
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
}
Open your storyboard, select your view controller and in attributes inspector, uncheck Under Top Bars in View Controller group

How to add scope buttons in a UISearchController embedded in UINavigationController

I have an App that is presenting a MKMapView embedded in a UINavigationController. In the UINavigationController I have put a UISearchController. When the User touch the UISearchController it displays a UITableViewController.
It works well while I'm not adding the Scope button in the UISearchController.
Here the screenshot of the UISearchController in the UINavigationController when I start the App.
Next when I touch the UISearchController, it displays the UITableViewController and scope button.
Here we can already see there's an issue with the scope button because they are not well integrated in the UISearchController (color should be translucent)
Next, when I touch the Cancel button to go back to the Main viewController, the UISearchController is not recovering its original style
it has a dark gray border (that probably comes from the scope button).
Here's how I add the UISearchController in the Main view Controller
func initSearchController() {
let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController
self.searchController = UISearchController(searchResultsController: mySearchController)
mySearchController.theSearchController = self.searchController
mySearchController.delegate = self
// Configure the UISearchController
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.placeholder = "data.."
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
this method is called in the viewDidLoad() of my Main ViewController.
Next, when the SearchController is displayed, I'm adding the scope button with the following code in my TableViewController subclass
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Mandatory to make sure the TableView is displayed when the search field is empty
// when user touch it.
view.hidden = false
var rect = delegate.searchController.searchBar.superview?.frame
rect?.size.height = 88
self.delegate.searchController.searchBar.scopeButtonTitles = ["one", "two", "three"]
self.delegate.searchController.searchBar.showsScopeBar = true
self.delegate.searchController.searchBar.superview?.frame = rect!
}
and the following code is executed when search is closed
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
var rect = delegate.searchController.searchBar.superview?.frame
rect?.size.height = 44
self.delegate.searchController.searchBar.superview?.frame = rect!
self.delegate.searchController.searchBar.showsScopeBar = false
self.delegate.searchController.searchBar.scopeButtonTitles = nil
}
As you can see I have severals issues with this code.
Scope buttons are not displayed correctly and I'm unable to add them with a nice animation
When user exits the search Scope buttons are removed but it impacts the background of the UISearchController
Can you tell me what I'm doing wrong and what should I do to integrate correctly Scope Button in UISearchController?.
I have found examples but only when the UISearchController is not embedded in the UINavigationController.
Thanks for your help!
Sébastien.
You should try using the searchBar.scopeButtonTitles in your instance of UISearchController:
func initSearchController() {
let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController
searchController = UISearchController(searchResultsController: mySearchController)
// Set Scope Bar Buttons
searchController.searchBar.scopeButtonTitles = ["one", "two", "three"]
// searchController.searchBar.showsScopeBar = true //if you want it always visible
// Configure the UISearchController
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = "data.."
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
}
No need to show or hide your scopeButtons in willAppear/didDisapear. This is set by: searchController.searchBar.showsScopeBar = true

swift search Bar and navigation bar will be hidden after starting

in my swift 2 app i have a table view with a search bar:
But if tap on the search bar, my navigation bar and the search bar will be hidden.
This is my code which is in the viewDidLoad
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.hidesNavigationBarDuringPresentation = true
self.MyTable.tableHeaderView = controller.searchBar
return controller
})()
At the beginning i also get this message:
Attempting to load the view of a view controller while it is
deallocating is not allowed and may result in undefined behavior
()
My Question is, where is my mistake? :/
from this tutorial you can use this code to solve the problem.
self.navigationController!.navigationBar.translucent = false
searchController!.hidesNavigationBarDuringPresentation = false
// This makes the view area include the nav bar even though it is opaque.
// Adjust the view placement down.
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = UIRectEdge.Top
You have to modify your code with the below if you want to make it worked perfectly:
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.searchBar.delegate = self
self.definesPresentationContext = true
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.hidesNavigationBarDuringPresentation = true
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.resultSearchController
} else {
self.tableView.tableHeaderView = self.resultSearchController.searchBar
}

Resources