How to change separator height in UITableView Swift 3?

前端 未结 4 1281
抹茶落季
抹茶落季 2021-01-07 05:59

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 heig

相关标签:
4条回答
  • 2021-01-07 06:40

    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.

    0 讨论(0)
  • 2021-01-07 06:47

    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)
    }
    
    0 讨论(0)
  • 2021-01-07 06:56

    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
        }
    
    0 讨论(0)
  • 2021-01-07 07:00

    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
    }
    
    0 讨论(0)
提交回复
热议问题