Can't get rid of space at top of UITableView

I have a UITableView inside a UIViewController (not UITableViewController) with a UISearchBar at the top of the table view that's shown and hidden via a search button on the UINavigationBar. When you scroll to the top the table view's background is shown above the search bar. I've been doing a lot of research and can't figure this out. PLEASE HELP!!

http://i1152.photobucket.com/albums/p493/-roar/Image-1_zpswgtd0w6d.jpg

make sure your ad blocker is on before clicking the link. lots and lots of ads

Replies

Could not access to your image (those ads !). So it is difficult to understand what the problem is.

When you hide the search bar, do you want to move the tableView to the top ?

Maybe in that case you can modify searchBar.frame.size.height ? I've tried but I get some bizarre effects.


Or just hide the background ?

If so, you could set tableView background alpha to zero ?


    @IBAction func searchToggle(_ sender: UIBarButtonItem) {
   
        searchBar!.isHidden = !searchBar!.isHidden
        if searchBar!.isHidden {
            searchBar!.isTranslucent = true
             theTableView.backgroundColor = theTableView.backgroundColor?.withAlphaComponent(0.0)
        } else {
            searchBar!.isTranslucent = false
            theTableView.backgroundColor = theTableView.backgroundColor?.withAlphaComponent(1.0)
        }
    }


To move table when searchBar is hidden, I tried this:

    @IBAction func searchToggle(_ sender: UIBarButtonItem) {
       
        searchBar!.isHidden = !searchBar!.isHidden
        if searchBar!.isHidden {
            searchBar!.isTranslucent = true
            theTableView.backgroundColor = theTableView.backgroundColor?.withAlphaComponent(0.0)
            theTableView.frame.origin.y = 22
        } else {
            searchBar!.isTranslucent = false
            theTableView.backgroundColor = theTableView.backgroundColor?.withAlphaComponent(1.0)
            theTableView.frame.origin.y = 76
        }
    }

Here's what I have. In viewDidLoad, I make change the table view's content inset to the bottom on the search bar:


tableView.contentInset = UIEdgeInsetsMake(-searchBar.bounds.height, 0, 0, 0)


That work's great. No issues here. Here's the search button function so far:


@IBAction func searchButton(_ sender: UIBarButtonItem) {
     
        if (searchBar.isHidden == true){         
            searchBar.isHidden = false
            tableView.contentInset = UIEdgeInsetsMake(22, 0, 0, 0)
            let rect = CGRect(origin: searchBar.subviews.first!.center, size: CGSize(width: searchBar.bounds.width, height: searchBar.subviews.first!.bounds.height))
            tableView.scrollRectToVisible(rect, animated: true)
        }
         
        else{
            searchBar.isHidden = true
            tableView.contentInset = UIEdgeInsetsMake(-searchBar.bounds.height, 0, 0, 0)
        }
    }


When I show the search bar it scrolls to the top of the bar perfectly. BUT! As soon as you scroll a little bit to the top you see the table view's background. Just made a video of the issue. https://youtu.be/pBGMZOtmpPs



HEY Claude!!

That seems a very normal behavior. It's intended to let you know you have dragged too low and the spring will bring it back.


I've always found this pretty natural.


If the red color bother you, you can always set the alpha to 0, as I described before.

I tested what you said, and it never corrects itself. I let it sit for a full minute and nothing happened. What you're saying makes sense, but it's not doing that.


I set the color to red to give it contrast.

>I tested what you said, and it never corrects itself.


Create a new project via the 'single view' template, copy just your tableview from your working project and paste it into the sv project - does spring back work there?

Even though the value you use seem logic, could you try to change the value from -searchBar.bounds.height to something different ?


tableView.contentInset = UIEdgeInsetsMake(-searchBar.bounds.height, 0, 0, 0)


As -(searchBar.bounds.height - 10) or - 20 or - searchBar.bounds.height / 2 (I tried to measure the remaining red zone)


tableView.contentInset = UIEdgeInsetsMake(-searchBar.bounds.height / 2, 0, 0, 0)

That's one of the first things I tried. I used this code for this example:


tableView.contentInset = UIEdgeInsetsMake(searchBar.bounds.height - 15, 0, 0, 0)


https://youtu.be/fz4F3AtEPZg

I had the same problem in my app : "extra" padding between searchBar bottom and tableview top. I solved it that way :

if #available(iOS 15, *) {
            self.tableView.sectionHeaderTopPadding = 0
}

Hope that helps ^^