Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to increase the UITableView separator line thickness?

I have looked everywhere. Trying to increase the thickness of this line. Is there anyway to do this programmatically? Thanks

like image 325
Kex Avatar asked Feb 21 '15 14:02

Kex


3 Answers

My solution to this was to add an extension to either UIView or a UITableViewCell.

extension UIView {

func addSeparator(ofHeight height : CGFloat) {
    let lineView = UIView()
    lineView.backgroundColor = .red
    self.addSubview(lineView)
    let constraintString = "V:|-\(self.frame.size.height - height)-[v0(\(height))]|"
    self.addConstraintsWithFormat("H:|[v0]|", views: lineView)
    self.addConstraintsWithFormat(constraintString, views: lineView)
}

//MARK: - Constraints Extension

func addConstraintsWithFormat(_ format: String, views: UIView...) {
    var viewsDictionary = [String: UIView]()
    for (index, view) in views.enumerated() {
        let key = "v\(index)"
        view.translatesAutoresizingMaskIntoConstraints = false
        viewsDictionary[key] = view
    }
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
} }

Then use it within your custom TableViewCell or any View you would like to add a bottom line to.

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    self.addSeparator(ofHeight: 1)
}
like image 60
Russell Warwick Avatar answered Nov 16 '22 02:11

Russell Warwick


The only way of doing it, is setting the separtorStype to UITableViewCellSeparatorStyleNone and then you have two options:

  • Create a custom UITableViewCell with the separator inside it or
  • Create an alternate UITableViewCell with the separator you want and place inside every other cells. If you want to display 3 rows on your table, you should display 5 instead with the alternate cell in rows 2 and 4.
like image 42
jherran Avatar answered Nov 16 '22 03:11

jherran


private let kSeparatorId = 123
private let kSeparatorHeight: CGFloat = 1.5

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
    if cell.viewWithTag(kSeparatorId) == nil //add separator only once
    {
        let separatorView = UIView(frame: CGRectMake(0, cell.frame.height - kSeparatorHeight, cell.frame.width, kSeparatorHeight))
        separatorView.tag = kSeparatorId
        separatorView.backgroundColor = UIColor.redColor()
        separatorView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

        cell.addSubview(separatorView)
    }
}
like image 32
ChikabuZ Avatar answered Nov 16 '22 02:11

ChikabuZ