Search code examples
iosswiftuitableviewseparator

UITableView separatorStyle = none vs. separatorColor = .clear


Yesterday I took a look at a quite big project from someone else and I noticed that wherever the intention was not to have the separator generated by UITableView, it is done by setting tableView.separatorColor = .clear instead of tableView.separatorStyle = .none.

Now I wondered If there might be a reason for that.. Is the result any different or does changing the style property mess up the constraints somehow because 0.5p are missing?


Solution

  • Yes, you are thinking in right direction.

    tableView.separatorColor = .clear // It clears the separator background color

    tableView.separatorStyle = .none // It removes the separator from superview(UITableCell)

    Case Study:

    Consider height of UITableViewCell is set to 50.

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    

    If there are a UILabel in UITableViewCell and you have provided the leading, trailing, top and bottom constraint then tableView.separatorStyle = .none will not cause any constraint-break as height of UILabel will be auto-increased.

    But if above case if you have applied height-constraint as well, then there will be difference of 0.5 pixel in calculated height and constraint height.

    It is not necessarily happen every-time but to prevent this condition, we should use tableView.separatorColor = .clear.