3
votes

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:

enter image description here

After pressed back button from the detailViewController:

The first index is covered by the navigation controller

enter image description here

1
Is the row really there, and covered? Or is "babi" the only row in the table? In other words, when you "come back" from the "profile" view, can you pull down on the table to see the top row (containing "jajang")?DonMag
@DonMag Yes, the top row is 'jajang' but the top row is covered by the navigation bar (After I pressed back button from the detail view). Meaning I can still scroll down. In the beginning wasn't like that tho.Buka Cakrawala
ok - and when you scroll down, and release your finger... does the top row remain visible? Or does it "snap back up" under the nav bar?DonMag
it snaps back up under the nav bar. But I'm pretty sure I didn't change the height or constraint anythingBuka Cakrawala
I'd suggest: when you first see the table, and it looks correct, go into Debug View Hierarchy and note the constraints and values... When you "come back" and the top row is under the Nav Bar, Debug View Hierarchy again and look for differences. Also... make sure your code is not adding multiple table views.DonMag

1 Answers

0
votes

Go to storyboard.

Select the ViewController (not the table view or the navigation controller).

In the attributes inspector, uncheck Adjust Scroll View Insets