我正在使用编程约束创建UITableViewCell.这通电话有cardView分和button分.仅使用cardview就可以正常工作.但一旦我将UIButton加到那里,我就可以在控制台上看到错误.

Complete Code:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    private 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() {
        super.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 section: 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("selected: \(indexPath.row)")
    }
}
 
private final class Cell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor  = .clear
        self.selectionStyle = .none
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private 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
    }()
 
    private(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: .normal)
        button.imageView?.tintColor = UIColor.lightGray
        return button
    }()
     
     
    func configure() {
         
        self.cardView.backgroundColor = .white
        self.buttonOptions.isHidden = false
    }
}

Error:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x6000005b7480 V:|-(9)-[UIView:0x7ff5ac9066e0]   (active, names: '|':UITableViewCellContentView:0x7ff5ac90d4d0 )>",
    "<NSLayoutConstraint:0x6000005b7520 UIView:0x7ff5ac9066e0.bottom == UITableViewCellContentView:0x7ff5ac90d4d0.bottom - 9   (active)>",
    "<NSLayoutConstraint:0x6000005b7570 UIView:0x7ff5ac9066e0.height == 60   (active)>",
    "<NSLayoutConstraint:0x60000059c3c0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff5ac90d4d0.height == 44   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000005b7570 UIView:0x7ff5ac9066e0.height == 60   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

在单元格配置中,如果我注释为self.buttonOptions.isHidden = false,则不会看到任何警告.我不知道我在这里遗漏了什么,请帮我解决这个问题.

推荐答案

乍一看,默认表视图高度(44)和自定义表视图高度(60)之间存在冲突.

然后我在UIViewAlertForUnsatisfiableConstraints点下降了一个转折点.太奇怪了,它在button.imageView号房.在我看来,它有正确的限制.把这一行注释掉,它就像符咒一样起作用.

//button.imageView?.tintColor = UIColor.lightGray

结论:我认为这是一个系统错误,理论上,系统高度(44)优先,其优先级将是cardView0.因此,一种解决办法是降低cardView的优先级.然后,警告就消失了.即:

var height = view.heightAnchor.constraint(equalToConstant: 60)
height.priority = .init(999)
height.isActive = true

我希望这会对你有所帮助.

Ios相关问答推荐

验证开发人员应用程序证书是否已在您的设备上验证

CIKernel的Metal Shader裁剪问题

分配给指针会通过 class_addMethod 导致 EXC_BAD_ACCESS

如何使用 SwiftData 从列表中删除子项目?

DllImport with .a file for iOS in MAUI

弹出视图后使所有背景 UI 模糊

SwiftUI:如何用拇指移动滑块值

Xcode 14 弃用了位码 - 但为什么呢?

将 SwiftUI 与 @AppStorage 和动态函数调用一起使用

禁用 UITextField 的自动更正

UICollectionView 添加 UICollectionCell

在 Swift 中按下返回键时在文本字段之间切换

如何更新推送通知服务证书

动画 UILabel 字体大小更改

在 Swift 中禁用向后滑动手势

在滚动视图中使用动态大小的控制器调整容器视图的大小

Xcode 4.2 如何将一个项目包含到另一个项目中?

如何在 Objective-C (iOS) 中的图像上写文本?

从 Swift 转换为 Objective-c 的工具

请在您的 Podfile 中为此目标指定一个平台?