How to change separator height in UITableView Swift 3?

随声附和 提交于 2019-12-19 04:50:52

问题


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
}


来源:https://stackoverflow.com/questions/44711612/how-to-change-separator-height-in-uitableview-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!