How to change separator height in UITableView Swift 3?

前端 未结 4 1282
抹茶落季
抹茶落季 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.

提交回复
热议问题