So I have a tableview under a navigation bar that will show up when the user is typing something in the search bar. And everything seems to work find. But my tableview is messed up after I push a view controller, when the user is selecting a cell in the tableview, then went back from the detailViewController.
• Problem: The most top cell of the tableview is covered by the navigation bar, after I pressed back in the detailViewController. But this doesn't happen in the beginning.
• I've tried:
1. Toggled 'Under Top Bars' in the storyboard
2. Check all my constraint
This is how added the tableview onto the subview
func updateSearchResults(for searchController: UISearchController) {
// When the user is typing
if let searchText = searchController.searchBar.text, !searchText.isEmpty {
// add the tableview onto the subview
view.addSubview(tableView)
self.searchController.dimsBackgroundDuringPresentation = false
// filter the username array, to match the text in the search bar
filteredUsernames = unfilterdUsernames.filter({ (username) -> Bool in
return username.lowercased().contains(searchText.lowercased())
})
if let usernames = filteredUsernames {
getUsersFromUsernames(usernames: usernames, completion: { (users) in
self.filteredUsers = users
})
}
// else, the user is not typing anything. Remove the tableview from the subview
} else {
self.searchController.dimsBackgroundDuringPresentation = false
tableView.removeFromSuperview()
filteredUsernames = unfilterdUsernames
}
// reload tableview data
tableView.reloadData()
}
This is how I move into the detailViewController:
tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
// the profile view that I want to display, after the user select one of the tableViewCell
let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateViewController(withIdentifier: "profile") as? ProfileCollectionViewController
// user is the value that I want to transfer to the profileController
// which user from the indexPath in tableView
if let user = filteredUsers?[indexPath.row] {
userToPass = user
// set the delegate to self
profileController?.userDataDelegate = self
// push a navigationController to profileController
self.navigationController?.pushViewController(profileController!, animated: true)
Before going to the detailViewController:
After pressed back button from the detailViewController:
The first index is covered by the navigation controller