1

I have some weird behaviour on a TableView and hope someone could help me out.

I have a TableView with a searchbar in the navigation. The cells lead to a detail view. Whenever I search in the TableView and then hit a result cell to go to the detail view, on returning from the detail view to the TableView the first cell disappears underneath the opened search bar.

Searching:

Searching

On selecting the item (detail view) and then returning (using the nav bar) the first cell is hidden under the search bar:

Returning

Some things to note:

  • If I print the contentInset on returning it gives -44 (1 cell hidden, like in the pic). I'm unable to set the contentInset back to 0.0 (or anything else for that matter) as it doesn't seem to change anything.
tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: true) print(tableView.contentOffset.y) // -44.0 

If the first cell is hidden and I hide the search bar by pushing the table down and then up again, the problem automatically disappears and the ContentInset restores to 0.0.

UPDATE: I found that the view restores itself as well after returning the app from the background:

enter image description here

I can't however reproduce that in code. I've tried:

view.setNeedsLayout() view.layoutIfNeeded() tableView.reloadData() 
8
  • 1
    I guess it is because of scrollview insets. Checkout this answer stackoverflow.com/questions/24335497/… Commented Apr 7, 2019 at 16:24
  • There are other ways to solve it but above seems simplest one. Commented Apr 7, 2019 at 16:25
  • How have you implemented your tableView? Are you using a UITableViewController or manually adding a UITableView? Commented Apr 7, 2019 at 16:27
  • @Rahul: that doesn't seem to be the same issue as mine. I already tried toggling the automaticallyAdjustsScrollViewInsets. Commented Apr 7, 2019 at 16:40
  • @RichAppz UITableViewController. I add the search bar programmatically in the viewDidLoad. I have the same implementation in other views as well where it doesn't have the same error, maybe it's the detail view that's causing it. Commented Apr 7, 2019 at 16:42

2 Answers 2

1

Solved, I set searchController.hidesNavigationBarDuringPresentation = false, which caused the bug. See the answer on this post: Search Bar in a Navigation Item collapses and gets stuck under status bar upon navigation pop, on iOS 11.

Thanks for the help!

Sign up to request clarification or add additional context in comments.

Comments

0

Put dismiss(animated: true) in tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.