ios – sendSubviewToBack is not working in UITableView


I want to adjust tableView’s hierarchy. I used both sendSubviewToBack and bringSubviewToFront to make sure RedCell is under BlueCell, also I create the RedCell first so that BlueCell should beyond RedCell.(the overBgView in RedCell should not cover BlueCell) However, the result shows below, it’s not working. So how to make sure the RedCell is under BlueCell?:
enter image description here
My code is here:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("<<< \(indexPath)")
        
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: RedCell.identifier, for: indexPath) as! RedCell
            // RedCell
            tableView.sendSubviewToBack(cell)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: BlueCell.identifier, for: indexPath) as! BlueCell
            // BlueCell
            tableView.bringSubviewToFront(cell)
            return cell
        }
    }
    
    private lazy var tableView: UITableView = {
        let table = UITableView(frame: .zero, style: .plain)
        table.estimatedRowHeight = 80
        table.showsVerticalScrollIndicator = false
        table.separatorStyle = .none
        table.delegate = self
        table.dataSource = self
        table.register(RedCell.self, forCellReuseIdentifier: RedCell.identifier)
        table.register(BlueCell.self, forCellReuseIdentifier: BlueCell.identifier)
        return table
    }()
    
    private lazy var label: UILabel = {
        let label = UILabel()
        label.text = "------split----the views below are not table cell"
        label.textColor = .black
        return label
    }()
    
    private lazy var view1: UIViewOne = {
        let view = UIViewOne()
        return view
    }()
    
    private lazy var view2: UIView = {
        let view = UIView()
        view.backgroundColor = .blue
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(RedCell.self, forCellReuseIdentifier: "redCell")
        tableView.register(BlueCell.self, forCellReuseIdentifier: "blueCell")
        setupUI()
    }
    
    public init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    private func setupUI() {
        view.addSubview(tableView)
        view.addSubview(label)
        view.addSubview(view1)
        view.addSubview(view2)
        
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().inset(200)
            make.left.right.equalToSuperview()
            make.height.equalTo(120)
        }
        
        label.snp.makeConstraints { make in
            make.top.equalTo(tableView.snp.bottom).offset(30)
            make.centerX.equalToSuperview()
            make.height.equalTo(20)
        }
        
        view1.snp.makeConstraints { make in
            make.top.equalTo(label.snp.bottom).offset(30)
            make.left.right.equalToSuperview()
            make.height.equalTo(44)
        }
        view2.snp.makeConstraints { make in
            make.top.equalTo(view1.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(44)
        }
    }
}

class RedCell: UITableViewCell {
    static let identifier = "RedCell"
    
    private lazy var overBgView: UIView = {
        let view = UIView()
        view.backgroundColor = .yellow
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .red
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(overBgView)
//        contentView.sendSubviewToBack(overBgView)
        self.superview?.sendSubviewToBack(overBgView)
        
        overBgView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(60)
        }
    }
}

class BlueCell: UITableViewCell {
    static let identifier = "BlueCell"
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .blue
        setupUI()
    }

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

class UIViewOne: UIView {
    private lazy var oversizeView: UIView = {
        let view = UIView()
        view.backgroundColor = .yellow
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .red
        addSubview(oversizeView)
        oversizeView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(60)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img