UISearchController searchBar move down when search is active - swift

I have presented view controller, which has table view in it.
I have added UISearchController in table header view. When search bar is active, it move down some pixel.I have tried all the options, none are working.
Below my code to add search controller in table view
searchController = UISearchController.init(searchResultsController: nil)
searchController?.searchResultsUpdater = self
searchController?.searchBar.sizeToFit()
searchController?.searchBar.delegate = self
searchController?.searchBar.searchBarStyle = UISearchBarStyle.default
searchController?.dimsBackgroundDuringPresentation = false
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.definesPresentationContext = true
searchController?.searchBar.placeholder = "Search"
self.otlTableView?.tableHeaderView = searchController?.searchBar
I am also attaching screenshots.
Searchcontroller1
Searchcontroller2

Related

Why is the search bar pushing the navigation bar out of the picture?

I added a UISearchBar programmatically to my view and every time you activate the searchBar the navigation bar is pushed out of the picture.
My view's hierarchy
searchController.searchBar.delegate = self
navigationItem.title = navigationItem.title ?? ci("plan_p")
tableView.rowHeight = 100.0
tableView.tableHeaderView = searchController.searchBar
tableView.setContentOffset(CGPoint(x: 0.0, y: (self.tableView.tableHeaderView?.frame.size.height)!), animated: false)
guard let projectId = GlobalState.selectedProjectId, let byProject : Results<Structure> = self.by(projectId: projectId) else { return }
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Suche nach Plan"
definesPresentationContext = true
Probably your UITableView is top of the view hierarchy (or more components in top of the view hierarchy).
If your tableView in top of the view hierarchy you should change the tableView's hierarchy in your Interface Builder for solve this issue.
You can simply put your tableView in another UIView component for solve this issue.

Adding searchBar to tableHeaderView adds bottom empty space

I need to add UISearchController to UIViewController I know that the best way to do it is adding it to UINavigationController like this:
let resultSearchController = UISearchController(searchResultsController: athkarSearchTable)
resultSearchController?.searchResultsUpdater = athkarSearchTable
navigationItem.searchController = resultSearchController
The problem is I can't use UINavigationController so I found another solution: adding it to tableHeaderView like the following:
let resultSearchController = ({
let controller = UISearchController(searchResultsController: athkarSearchTable)
controller.searchResultsUpdater = athkarSearchTable
// to give a semi-transparent background when the search bar is selected.
controller.dimsBackgroundDuringPresentation = true
controller.searchBar.sizeToFit()
controller.searchBar.barStyle = UIBarStyle.default
controller.searchBar.searchBarStyle = .minimal
tableView.tableHeaderView = controller.searchBar
return controller
})()
// to limit the overlap area to just the View Controller’s frame instead of the whole Navigation Controller
definesPresentationContext = true
However, this solution adds an empty space to the bottom of the tableView in the UIViewController not the athkarSearchTable which is not good :/ as the screenshot below:
I tried to set the footer to an empty view and to set the height of footer to zero, none of them worked :/
Any suggestion how to remove the bottom empty space?
Problem solved by adding the following lines:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 400

UISearchController shifted up when typing under UINavigationController

I embedded a viewcontroller(BlueViewController) in a navigation controller.
the blueViewController has a UISearchController that set as the blueViewController's tableView headerView.
the searchController works fine except when it's active, it shift up and hide behind the UINavigationController(or somewhere)
I've tried to add the search controller's searchBar as a subview to the viewController, or positions a UIView and assigned the searchBar to that UIView, none of these works
I have tried this link, doesn't work
Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar
here is the searchController in BlueViewController
let searchController:UISearchController = {
let controller = UISearchController(searchResultsController: nil)
controller.hidesNavigationBarDuringPresentation = false
controller.obscuresBackgroundDuringPresentation = false
controller.searchBar.inputAssistantItem.leadingBarButtonGroups = []
controller.searchBar.inputAssistantItem.trailingBarButtonGroups = []
return controller
}()
In viewDidLoad
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
definesPresentationContext = true
you can also find entire project here
https://github.com/QiquanLu/TestNavigationWithSearchController
Any hint would be appreciated, thanks!
For iOS 11 and above, you should be setting the searchController property on BlueViewController's navigationItem like so:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
}
Don't add it as the tableView's header.

Search Controller .dimsBackgroundDuringPresentation also dims Status Bar?

I have a function to set up my Search Controller which I add to View Did Load.
func setUpSearchController() {
searchController.delegate = self
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = true
searchController.searchBar.sizeToFit()
searchController.searchBar.backgroundColor = .white
searchController.searchBar.barTintColor = .white
searchController.searchBar.placeholder = "Search"
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.tintColor = Colours.brandGreen
definesPresentationContext = true
messagesTableView.tableHeaderView = searchController.searchBar
}
When clicking on the Search Bar the Search Controller dims the background as expected by
searchController.dimsBackgroundDuringPresentation = true
The issue is it also dims the status bar making the UI look ugly. Anyone know how to exclude the status bar from also being dimmed?
As always any help greatly appreciated. Illustration below.
Update
I realise I'm using the Search Controllers Search Bar inside the TableView Header and changing this from
messagesTableView.tableHeaderView = searchController.searchBar
to
navigationItem.searchController = searchController
I get the desired result. However, I'd still like the Search Controllers Search Bar inside the Table Views header.

iOS8 Swift UISearchController hides navigationbar

I implemented a search function for an UITableViewController like this:
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
The problem I've been experiencing is that if I click on it, my navigation bar and my navigationcontrollers header disappear.
Is there a solution to stop this behaviour (in swift)?
Not clicked:
Clicked:
The UISearchController has a property called hidesNavigationBarDuringPresentation, maybe that can help you.
EDIT: Just tried it myself and it works, just add this line:
searchController.hidesNavigationBarDuringPresentation = false