How to add custom UIView on UITableViewCell Swipe in swift?

advertisements

I want to add custom UIView on UITableViewCell Swipe. Basically I want to add two UIButton, one on top and other just below it. Both having height half of cell. If I use editActionsForRowAtIndexPath delegate method of UITableView that this add UIButton side by side but i want to add buttons on top having height equal to half of table view cell and other just below it having same height.

Thanks in advance!!!


you can add this code in your custom tableview cell:

var topBtn = UIButton()
var behindBtn = UIButton()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    awakeFromNib()
}

override func awakeFromNib() {
    topBtn.setTitle("top", for: .normal)
    topBtn.backgroundColor = UIColor.blue
    addSubview(topBtn)

    behindBtn.setTitle("behind", for: .normal)
    behindBtn.backgroundColor = UIColor.green
    addSubview(behindBtn)
}

override func layoutSubviews() {
    super.layoutSubviews()
    let btnH = self.frame.size.height/2
    let btnW = self.frame.size.width

    topBtn.frame = CGRect(x: 0, y: 0, width: btnW, height: btnH)
    behindBtn.frame = CGRect(x: 0, y: btnH, width: btnW, height: btnH)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

and this is the result: