I have a UISearchController configured as searchController for navigation bar. I have following setup.
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = LocalisedStrings.searchChannels
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.delegate = self
navigationItem.hidesSearchBarWhenScrolling = false
navigationItem.searchController = searchController
I have a UITableView as child view for the ViewController's view as follows:
func setupView() {
extendedLayoutIncludesOpaqueBars = true
view.backgroundColor = secondarySurfaceColor
view.addSubview(channelsTV)
view.addSubview(emptyView)
NSLayoutConstraint.activate([
channelsTV.leadingAnchor.constraint(equalTo: view.leadingAnchor),
channelsTV.topAnchor.constraint(equalTo: view.topAnchor),
channelsTV.trailingAnchor.constraint(equalTo: view.trailingAnchor),
channelsTV.bottomAnchor.constraint(equalTo: view.bottomAnchor),
emptyView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
emptyViewCenterYAnchorConstraint,
])
}
I am facing the following issue of UITableView items visible under the search controller for minor scrolls and is normal during normal up/down scroll sling.
Desired behavior is that the tableView cells should not overlap the Search Bar and Navigation bar. But the navigation bar should collapse from large title to small title when tableView is scrolled. Is there any way? Help on this is much-appreciated thanks in advance. Please comment if you'd need some more details.
try this trick:
view.addSubview(UIView()) // To prevent the large tile nav bar from collapsing
I ignore the reason but this breaks the link between your the UIScrollView and the nav bar... This probably solve your issue.
Related
I have a UIViewController with an UITableView added on it’s subview. Everything was working fine until I added a UISearchController
When I add the searchController to the navigation item:
navigationItem.searchController = searchController
it seems the contentInset of the TableView breaks.
This is how the UISearchController is setup
private lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "test"
definesPresentationContext = true
return searchController
}()
And this is the constraints
NSLayoutConstraint.activate([
tableView.leftAnchor.constraint(equalTo: view.leftAnchor),
tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
On the left side is the one with the searchController installed on the right without. You can see that the table contentOffset is different.
I even tried playing around with things like extendedLayoutIncludesOpaqueBars or edgesForExtendedLayout but it doesn’t seem to work.
I’m close to giving up and use a UISearchBar instead but I’d lose some nice animations and convenience to hide/show the bar, etc.
Any tips on what I could do to fix this issue? Thanks
I am running into a small problem, I implemented the new iOS 11's style search bar in my app, and I noticed that it disappeared with a slightly different animation from the one in Messages for example. It's faster and less smooth.
Anyone has ever stumble upon this "problem" ?
Here is the code I use :
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = .white
searchController.searchBar.layer.borderColor = UIColor.white.cgColor
searchController.searchBar.layer.borderWidth = 1
}
definesPresentationContext = true
searchController.searchBar.placeholder = "all_search".localized
EDIT:
I don't know if it will help you, but I am scrolling at a normal pace .
Thanks
Adding
self.extendedLayoutIncludesOpaqueBars = true
to my viewDidLoad solved the issue, your navigation bar must not be translucent and note that extendedLayoutIncludesOpaqueBars = true is being attributed to my main view which holds the tableview.
This happens when your table view doesn't go all the way to the top of the view. Make sure your table view is "behind" the navigation bar and uses extended edges and extend under opaque edges if your navigation bar is opaque.
Try this, it fixes it for me. I used a different UIViewController as the searchResultsUpdater and just set extendedLayoutIncludesOpaqueBars as true.
searchResultsUpdater.extendedLayoutIncludesOpaqueBars = true
searchController.searchResultsUpdater = searchResultsUpdater
UIView.animate(withDuration: 1, animations: {
//your codes to implement
}, completion: nil)
change withDuration: in seconds
Custom UITableViewController for UISearchController overlays the search bar in iOS 11 when you set the search controller to the navigationItem.searchController. The table view controller covers the whole screen so you cannot see the search bar while typing. This is not an issue when you send nil to UISearchController. Basically, I have a search bar for this map app, so I am setting a UITableViewController to the UISearchController to handle the display of the search result when searching for location. Previously, the table view shows up at the bottom of the search bar, now it covers the whole screen.
Here's a code snippet:
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchBar.sizeToFit()
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = true
searchController.definesPresentationContext = false
searchController.searchResultsUpdater = self
searchController.delegate = self
if #available(iOS 11.0, *){
self.navigationItem.searchController = searchController
}
Found the issue. Need to set the right origin value in willPresentSearchController
I have a UINavigationController that has a UISearchController assigned to the navigationItem's searchController. Inside the view I have a UICollectionView. The search bar displays in the navigation bar, but when I scroll, the navigation bar automatically hides. Can I stop this from happening?
let searchController = UISearchController(searchResultsController: recentSearchesViewController)
searchController.searchResultsUpdater = recentSearchesViewController
searchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
navigationItem.searchController = searchController
You have to set :
navigationItem.hidesSearchBarWhenScrolling = false
I'm trying to keep the search bar in view as the table scrolls. At the moment I'm placing it as the header in a tableview, and it works as it should, but of course the search bar scrolls off screen as you go down the table. I thought I could do this simply modifying this code sample:
How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
tableview.tableHeaderView = searchContoller.searchBar // How to put it elsewhere?
//Alternative that also works
navigationItem.titleView = searchController.searchBar
The idea was to take some other view and do
otherview = searchController.searchBar
For instance an outlet to a UISearchBar, or a blank UIView, or something like that.
But it doesn't show the searchBar if I do that. It seems to only work as the header view of a table or as a navigationItem.titleView.
Am I missing something?
If you have a blank UIView that is placed above the tableview.
let's assume you have an outlet to that blank UIView called searchContainer.
Then you can add the search bar of the UISearchController to that view by adding the following line
searchContainer.addSubview(searchController.searchBar)