How to implement scrolling of the tableView to the top hiding the search bar in its tableheaderView?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to implement scrolling of the tableView to the top hiding the search bar in its tableheaderView?



I have already gone through some similar questions and tried implementing their solutions. However they seem to have some problem in especially iPhone X. I am using deployment target as 9.3.



To do so, I wrote this code but iPhone X puts tableView higher hiding more than half of the first row along with the search bar too.


override func viewDidAppear(_ animated: Bool)
self.tableView.contentOffset = CGPoint(x: 0.0,y: 0.0)




also the below snippet appears to have no effect at all:


self.tableView.scrollToRow(at: IndexPath( row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)



Adding full code if you want to have a look at it.


import UIKit

class CountryTableViewController: UITableViewController, UISearchResultsUpdating

let countriesArray = ["Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria", "Afghanistan", "Albania", "Algeria"]
let countrycodes = ["+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213", "+93", "+355", "+213"]
var filteredArray = [String]()
var searchController = UISearchController()
var resultsController = UITableViewController()

override func viewDidLoad()
super.viewDidLoad()

// Do any additional setup after loading the view.


let cancelButton: UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancel))
self.navigationItem.leftBarButtonItem = cancelButton

let searchButton: UIBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(searchButtonAction))
searchButton.image = UIImage(named: "search")
self.navigationItem.rightBarButtonItem = searchButton

searchController = UISearchController(searchResultsController: resultsController)
tableView.tableHeaderView = searchController.searchBar

searchController.searchResultsUpdater = self

resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self

automaticallyAdjustsScrollViewInsets = false



func updateSearchResults(for searchController: UISearchController)

filteredArray = countriesArray.filter( (countriesArray:String) -> Bool in
if countriesArray.contains(searchController.searchBar.text!)

return true

else

return false;


)
resultsController.tableView.reloadData()
automaticallyAdjustsScrollViewInsets = false


@objc func cancel()
self.dismiss(animated: true, completion: nil)


@objc func searchButtonAction()
self.dismiss(animated: true, completion: nil)
self.searchController.isActive = true
self.searchController.searchBar.text = ""


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if tableView == resultsController.tableView

return filteredArray.count

else

return countriesArray.count




override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

let cell = UITableViewCell()

if tableView == resultsController.tableView

cell.textLabel?.text = filteredArray[indexPath.row]
cell.detailTextLabel?.text = countrycodes[indexPath.row]
cell.imageView?.image = UIImage (named: "Flag0")

else

cell.textLabel?.text = countriesArray[indexPath.row]
cell.detailTextLabel?.text = countrycodes[indexPath.row]
cell.imageView?.image = UIImage (named: "Flag0")



return cell


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

print(indexPath.row, indexPath.section)




override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.

override func viewDidAppear(_ animated: Bool)

self.tableView.scrollToRow(at: IndexPath( row: 0, section: 0), at: UITableViewScrollPosition.bottom, animated: false)
// self.tableView.contentOffset = CGPoint(x: 0.0,y: 0.0)
// self.tableView.setContentOffset(.zero, animated: true)
//self.tableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0)
self.automaticallyAdjustsScrollViewInsets = false;


override func viewWillAppear(_ animated: Bool)

// self.tableView.scrollToRow(at: IndexPath( row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)
// self.tableView.contentOffset = CGPoint(x: 0.0,y: 44.0)
// self.tableView.setContentOffset(.zero, animated: true)











Do you add the search bar in the storyboard or programmatically?
– Kerberos
Aug 10 at 7:04





I implemented programmatically.
– Yoas Yere
Aug 10 at 7:06





Can you add the significant code?
– Kerberos
Aug 10 at 7:07





Added @Kerberos.
– Yoas Yere
Aug 10 at 7:12




1 Answer
1



you should add definesPresentationContext = true in your viewDidLoad().


definesPresentationContext = true





Doesn't work too
– Yoas Yere
Aug 10 at 10:32






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS