iOS Development

ios – UITableViewCell programmatical constrain situation

Spread the love


I I’m creating UITableViewCell utilizing programmatical constrains. The decision has a cardView and a button. The cardView alone works tremendous. However as soon as I add UIButton to there then I can see the error on the console.

Full Code:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    personal lazy var tableView: UITableView = {
    
        let view = UITableView()
        
        self.view.addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        view.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        view.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
        view.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true
        return view
        
    }()
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
    
        tableView.register(Cell.self, forCellReuseIdentifier: "Cell")
        tableView.separatorStyle = .none
        
        tableView.delegate = self
        tableView.dataSource = self
    }
 
     
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
          
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         
        let cell: Cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
        cell.configure()
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        print("chosen: (indexPath.row)")
    }
}
 
personal remaining class Cell: UITableViewCell {
    
    override init(fashion: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(fashion: fashion, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor  = .clear
        self.selectionStyle = .none
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
    
    personal lazy var cardView: UIView = {
       
        let view = UIView()
        self.contentView.addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
 
        NSLayoutConstraint.activate([
            
            view.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 18),
            view.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 9),
            view.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -18),
            view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -9),
            view.heightAnchor.constraint(equalToConstant: 60)
        ])
        
        view.layer.cornerRadius = 8
        view.layer.borderColor = UIColor.lightGray.withAlphaComponent(0.7).cgColor
        view.layer.borderWidth = 1.0
        return view
    }()
 
    personal(set) lazy var buttonOptions: UIButton = {
       
        let button = UIButton()
        self.cardView.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
         
            button.widthAnchor.constraint(equalToConstant: 22),
            button.heightAnchor.constraint(equalToConstant: 18),
            button.centerXAnchor.constraint(equalTo: self.cardView.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: self.cardView.centerYAnchor)
        ])
        
        button.backgroundColor = UIColor.lightGray.withAlphaComponent(0.6)
        button.setImage(UIImage(systemName: "ellipsis")!, for: .regular)
        button.imageView?.tintColor = UIColor.lightGray
        return button
    }()
     
     
    func configure() {
         
        self.cardView.backgroundColor = .white
        self.buttonOptions.isHidden = false
    }
}

Error:

[LayoutConstraints] Unable to concurrently fulfill constraints.
    Most likely not less than one of many constraints within the following checklist is one you do not need. 
    Do that: 
        (1) take a look at every constraint and take a look at to determine which you do not count on; 
        (2) discover the code that added the undesirable constraint or constraints and repair it. 
(
    "<NSLayoutConstraint:0x6000005b7480 V:|-(9)-[UIView:0x7ff5ac9066e0]   (energetic, names: '|':UITableViewCellContentView:0x7ff5ac90d4d0 )>",
    "<NSLayoutConstraint:0x6000005b7520 UIView:0x7ff5ac9066e0.backside == UITableViewCellContentView:0x7ff5ac90d4d0.backside - 9   (energetic)>",
    "<NSLayoutConstraint:0x6000005b7570 UIView:0x7ff5ac9066e0.peak == 60   (energetic)>",
    "<NSLayoutConstraint:0x60000059c3c0 'UIView-Encapsulated-Format-Peak' UITableViewCellContentView:0x7ff5ac90d4d0.peak == 44   (energetic)>"
)

Will try and get better by breaking constraint 
<NSLayoutConstraint:0x6000005b7570 UIView:0x7ff5ac9066e0.peak == 60   (energetic)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this within the debugger.
The strategies within the UIConstraintBasedLayoutDebugging class on UIView listed in <UIKitCore/UIView.h> may additionally be useful.

Within the cell config, If I remark self.buttonOptions.isHidden = false then I do not see any warnings. I’m not certain what’s the factor I’m lacking right here, Please assist me out to resolve this drawback.

Leave a Reply

Your email address will not be published. Required fields are marked *