How to change separator height in UITableView Swif

2019-02-15 23:55发布

问题:

Although there a few answers already on this topic. None of them cover Swift 3 and they are from a long time ago. What is currently the best way to change the separator height in a UITableView in Swift 3?

回答1:

Updated for Swift 3:

If you want to change the height of the UITableView separator, use the code below.
You should add it to the UITableViewCell method awakeFromNib() to avoid re-creation.

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    let mScreenSize = UIScreen.main.bounds
    let mSeparatorHeight = CGFloat(3.0) // Change height of speatator as you want
    let mAddSeparator = UIView.init(frame: CGRect(x: 0, y: self.frame.size.height - mSeparatorHeight, width: mScreenSize.width, height: mSeparatorHeight))
    mAddSeparator.backgroundColor = UIColor.brown // Change backgroundColor of separator
    self.addSubview(mAddSeparator)
}


回答2:

This is a correct way to do this.

First, in your ViewController you should set (tableView.separatorStyle = .none)

import UIKit 

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorStyle = .none
   }

}

Second, in your TableViewCell class your should create a separatorView. And don't forget to inherit TableViewCell class for your cell.

class TableViewCell: UITableViewCell {

   override func layoutSubviews() {
      super.layoutSubviews()

      //Your separatorLineHeight with scalefactor 
      let separatorLineHeight: CGFloat = 1/UIScreen.main.scale

      let separator = UIView()

      separator.frame = CGRect(x: self.frame.origin.x, 
                               y: self.frame.size.height - separatorLineHeight,
                           width: self.frame.size.width,
                          height: separatorLineHeight)

      separator.backgroundColor = .black

      self.addSubview(separator)
   }

}

Finally, you've got a thin separator line and, of course, you can increase this value what do you like.



回答3:

For Those who want to do it using autolayout here is the code

var additionalSeparator:UIView = UIView()
override func awakeFromNib() {
        super.awakeFromNib()
        self.createSeparator()
    }
    func createSeparator() {

        self.additionalSeparator.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(self.additionalSeparator)
    }
    func setConstraintForSeparator() {
        self.additionalSeparator.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: self.separatorInset.left).isActive = true
        self.additionalSeparator.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -self.separatorInset.right).isActive = true
        self.additionalSeparator.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0).isActive = true
        self.additionalSeparator.heightAnchor.constraint(equalToConstant: 1).isActive = true
        self.additionalSeparator.backgroundColor = UIColor.greyishBrown
    }


回答4:

Try this Swift 3:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: YOUR_CELL_IDENTIFIER, for: indexPath) as! yourTableViewCell

    let viewSeparatorLine = UIView(frame:CGRect(x: 0, y: cell.contentView.frame.size.height - 5.0, width: cell.contentView.frame.size.width, height: 5))
    viewSeparatorLine.backgroundColor = .red
    cell.contentView.addSubview(viewSeparatorLine)
    return cell
}