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
Related
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.
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'm having an issue with UISearchController. There have been some reports of erratic behavior in iOS 11, but none of the suggestions have fixed my problem.
The navigation bar is hidden in my app so I just want to place the search bar between two buttons on the main screen. I put UIView in the storyboard to serve as the superview for the search bar. When activated the results controller is a straight UITableViewController.
Everything is in-place when the app launches. When I access the search bar it just to the top of the screen, leaving it's parent view behind. Everything functions ok, but when I hide the table view, the search bar actual goes a bit lower than it was when it started. Here's the setup code:
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable
searchController = UISearchController(searchResultsController: locationSearchTable)
searchController.searchResultsUpdater = locationSearchTable
searchController.delegate = self
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
locationTableController = locationSearchTable
let searchBar = searchController!.searchBar
//searchBar.searchBarStyle = UISearchBarStyle.prominent
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
searchViewHolder.addSubview(searchBar)
searchBar.frame = searchViewHolder.bounds
searchBar.autoresizingMask = [.flexibleWidth, .flexibleHeight]
searchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = false
I have tried many, many approaches to fixing this. I'm wondering if I misunderstood an earlier suggestion. Any advice is welcome.
I'd the same issue. If your navbar is not translucent try to put this in viewDidLoad:
extendedLayoutIncludesOpaqueBars = true
I have a UITableViewController where I have placed a search bar in the tableHeaderView. It all looks fine like the picture below.
But, the problem is when it is presented:
As you can see, it is mostly hidden above the top of the screen.
Here is the code I used to set it up in the tableview controller's viewDidLoad:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Presets..."
searchController.searchBar.delegate = self
searchController.searchBar.barStyle = .blackTranslucent
searchController.hidesNavigationBarDuringPresentation = true;
self.tableView.tableHeaderView = searchController.searchBar
self.tableView.contentOffset = CGPoint(x:0, y:searchController.searchBar.frame.size.height);
If I print the frame of the searchbar, it's origin is (0,0).
I have tried everything I can think of, but nothing works
I figured out that if I check Translucent in the Navigation Bar's Attributes Inspector in IB the issue is resolved.
This leads me to believe that leaving that unchecked and setting extendedLayoutIncludesOpaqueBars to true would also work, but I found this not to be the case.