Search code examples
iosswiftuitableviewswift3list-separator

How to change separator height in UITableView Swift 3?


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?


Solution

  • 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)
    }