23

In iOS 15, UITableView adds a separator between a section header and the first cell:

enter image description here

How can I hide or remove that separator?

A few notes:

  1. The header is a custom view returned from tableView(_:viewForHeaderInSection:).
  2. When looking at the view debugger, I can see that the extra separator is actually a subview of the first cell, which now has a top and a bottom separator.
  3. Other than setting tableView.separatorInset to change the inset of cell separators, this is a completely standard table view with no customizations.
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Hesham
  • 5,294
  • 3
  • 34
  • 48

7 Answers7

13

Option 1: Maybe by using UITableViewCellSeparatorStyleNone with the table view and replacing the system background view of the cell with a custom view which only features a bottom line?

Option 2: Using hint from https://developer.apple.com/forums/thread/684706

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000 // only Xcode 13+ needs and can compile this
    if (@available(iOS 15.0, *)) {
        [self.tableview setSectionHeaderTopPadding:0.0f];
    }
#endif
}
MacMark
  • 6,239
  • 2
  • 36
  • 41
  • 4
    Option 2 does work! I tried changing the value of `sectionHeaderTopPadding` but didn't set it to 0. Option 1 should also work, but I was trying to avoid managing the separators myself. – Hesham Aug 26 '21 at 12:42
  • @user1561346 It is for me? Or do you mean the code in the thread is not working? – George Sep 06 '21 at 22:14
  • @user1561346 It looks like the link to the dev forum at Apple requires a login. – MacMark Sep 07 '21 at 08:38
  • 1
    I found this issue (additional space on top of table views) seems to be an issue with Xcode 13 since it does not occur when building an app with Xcode 12.5.1 and running it on iOS 15. It happens for me only when building with Xcode 13. – MacMark Sep 07 '21 at 08:52
  • Option 2 only works on plain style – user2027712 Sep 22 '21 at 10:22
  • Option 2 works on `.plain` style (I haven't tested this solution in other styles), and it also removes the separator below the last cell of the table. – Dhruv Saraswat Dec 17 '21 at 12:59
11

I had a similar issue, but it was due to the table header view suddenly showing as a separator on iOS 15. The only thing that worked for me was:

if #available(iOS 15.0, *)
{
    tableView.tableHeaderView = UIView()
}
Ivan Andriollo
  • 370
  • 1
  • 5
7

iOS 15, Swift 5

To remove the line between the section header and your first cell, you should set sectionHeaderTopPadding to zero while configuring your UITableView.

    if #available(iOS 15.0, *) {
        tableView.sectionHeaderTopPadding = 0.0
    }
Sajjad Sarkoobi
  • 827
  • 8
  • 18
1

I was able to fix it with this code.

if (@available(iOS 15.0, *)) {
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    [self.tableView setSectionHeaderTopPadding:0.0f];
}
Osamu
  • 29
  • 3
0

I believe tableView.separatorStyle = .none should do the trick.

Solomiya
  • 310
  • 4
  • 15
  • 1
    Setting `separatorStyle` to `none` will remove the separator below *all* the cells of the table, looks like the OP wants to remove the separator *only* below the header, not below *all* the cells. – Dhruv Saraswat Dec 17 '21 at 13:05
0

In my case I wanted to use the native section header and the top padding different than 0, so this should work if you want to remove the extra top and bottom separators on each section:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Dequeue cell and set it up ...
    if indexPath.row == 0 {
        let separator = cell.subviews.filter({
            $0.frame.minY == 0 && $0 !== cell.contentView
        }).first
        separator?.isHidden = true
        cell.separatorInset.left = 0 // Or whatever desired inset
    }
    if indexPath.row + 1 == dataSource[indexPath.section].count {
        cell.separatorInset.left = cell.bounds.width
    } else {
        cell.separatorInset.left = 0 // Or whatever desired inset
    }
    return cell
}
-1

There can be two solutions to your problem/

  1. One is

    self.tableView.separatorColor = self.tableView.backgroundColor

this is a trick solution, it makes outer lines "disappear" and keep separator lines visible

  1. second is

change your tableview type from grouped to plain if grouping not used.

Abu Ul Hassan
  • 1,340
  • 11
  • 28
  • The first solution will require that I manage separators manually since this will hide separators for all cells. Also, this table view is already using the plain style. – Hesham Aug 26 '21 at 12:39
  • it will not hide all just try that – Abu Ul Hassan Aug 27 '21 at 13:42
  • I did try it and it does hide the separators for me. I'm guessing that our table views are configured a bit differently. – Hesham Aug 29 '21 at 15:57