UISearchBar alignment issues in iOS 11 - swift

I have UITableViewController where i was adding UISearchController searchbar as an headerView and I have changed this to navigationItem.searchcontroller for iOS 11 in iOS 11 searchbar won't appear at all even navigationItem was present.
if don't use this searchbar UI is going for toss. Please suggest me what was i am doing wrong here.
let searchController = UISearchController(searchResultsController: vc)
searchController.searchBar.scopeButtonTitles = ["All", "images", "videos", "others"]
searchController.dimsBackgroundDuringPresentation = false
// Don't remove these two lines then search bar alignment issue will raise
self.extendedLayoutIncludesOpaqueBars = true
self.searchController.searchBar.isHidden = false
definesPresentationContext = false
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController // not working for me.
searchController?.hidesNavigationBarDuringPresentation = false
navigationItem.hidesSearchBarWhenScrolling = false
} else {
searchController?.hidesNavigationBarDuringPresentation = true
searchController?.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
}
List of issues are
on iOS 11 with UI goes wrong as attached image if add UISearchbar to header view
if I try add to navigation item as mentioned in the code it is not appearing
As i am using different search results controller when search is active i need to disable actions in present controller - how can i achieve this easily.

I'm not really sure what went wrong there but in my case, it's working fine. Here is my code:
Created a searchController file:
private let searchController = UISearchController(searchResultsController: nil)
In viewDidLoad():
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = kSearchPlaceholder
searchController.searchBar.scopeButtonTitles = [kCategory, kAuthor, kPicture]
searchController.searchBar.delegate = self
definesPresentationContext = true
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
And it's good to go.

Related

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.

Scope buttons hide under search bar on iOS 11

I have a search controller in my app which according to apple documentation on iOS 11 has to be added to the navigationItem's searchController property. I also have some scope buttons for the searchController. Everything works fine but when I rotate my device to landscape and rotate back to portrait then a wired thing happens. The scope buttons gets hidden under the search field. If I press the cancel button and tap on the search field again then it comes back to the normal state. The bug reproduces everytime only on iOS 11. The searchbar will look like this:
The normal state of the search controller:
This is how I add the searchController:
searchController.searchResultsUpdater = self as UISearchResultsUpdating
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = "Search device"
searchController.searchBar.scopeButtonTitles = ["Device", "Person", "OS version", "Serial"]
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.searchController
self.navigationItem.hidesSearchBarWhenScrolling = false
if let textField = searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundView = textField.subviews.first {
backgroundView.backgroundColor = UIColor.white
backgroundView.layer.cornerRadius = 10
backgroundView.clipsToBounds = true
}
}
} else {
tableView.tableHeaderView = searchController.searchBar
}
searchController.searchBar.delegate = self as UISearchBarDelegate

How to adjust constraints with collapsing SearchBar in large iOS11 NavigationBar

How do I adjust the constraint when pushing a new UIViewController when a UISearchController is implemented as self.navigationItem.searchController = search
New controller pushed: SearchBar collapses but leaves View with constrait to top = 0 on the initial position.
(The background of the ViewController is yellow for easy understanding of the matter)
Implementation:
viewDidLoad() {
if #available(iOS 11.0, *) {
let search = UISearchController(searchResultsController: nil)
search.searchResultsUpdater = self
search.definesPresentationContext = true
search.hidesNavigationBarDuringPresentation = false
search.searchBar.tintColor = UI.COLOR_WHITE
search.dimsBackgroundDuringPresentation = false
self.navigationItem.hidesSearchBarWhenScrolling = false
self.navigationItem.searchController = search
}
I have tried using self.automaticallyAdjustsScrollViewInsets = false without success and the tableView implements self.contentInsetAdjustmentBehavior = .always
What am I missing? Help is very appreciated.

After updating to iOS11 my searchBar behaves strange

After updating to iOS11 my searchBar behaves very strange. When activated, it jumps to the top of screen. It works as it should, as before but of course I want it to stay in place. I have tried a lot of solutions from googling this behavior but nothing helps. Do anyone have the same problem? And what did you do to solve this?
searchBar in place
searchBar jumped to top
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
// Setup the Search Controller
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.preservesSuperviewLayoutMargins = true
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
walkaboutTableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = matildaLightBlue
searchController.searchBar.clipsToBounds = true
searchController.searchBar.layer.borderWidth = 2
searchController.searchBar.layer.borderColor = UIColor.black.cgColor
}
Adding search controller instance into navigation item instead of table header view helps me to solve similar issue in my project.
if #available(iOS 11, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
navigationItem.searchController
navigationItem.hidesSearchBarWhenScrolling
Ruslan Kolosovskyi's answer is really good. In addition if you want the search bar to be more accessible:
if #available(iOS 11, *) {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
} else {
tableView.tableHeaderView = searchController.searchBar
}