选定单元格后如何向集合视图单元格添加约束?

问题描述

我正在尝试以编程方式创建功能,以便当用户在集合视图中选择一个单元时,该应用会保留所选图像的计数并将其添加为覆盖。如果选择的是视频,我还想将视频时长添加到图像的底部。我知道我的问题在于我的约束。您可以在下面的图像示例中看到,我试图将计数添加到集合视图单元格的左上角,但是当用户取消选择一个单元格时,该计数也会调整,例如,如果取消选择下面图像中的数字2数字3将变为2。在大多数情况下,我认为我的代码在工作,但我无法获得约束。使用当前配置,我遇到一个错误(请参阅下文),但我什至不知道从哪里开始这个问题。

“无法激活锚点的约束,因为它们具有 没有共同的祖先。约束或其锚点是否引用 不同视图层次结构中的项目?那是非法的。”

CollectionView:

func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? TestCVCell {
            cell.commonInit()

        }
    }
    
    func collectionView(_ collectionView: UICollectionView,didDeselectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? TestCVCell {
            //Not sure what to put here
        }
    }

覆盖

class CustomAssetCellOverlay: UIView {
    let countSize = CGSize(width: 40,height: 40)
    lazy var circleView: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        view.layer.cornerRadius = self.countSize.width / 2
        view.alpha = 0.4
        return view
    }()
    let countLabel: UILabel = {
        let label = UILabel()
        let font = UIFont.preferredFont(forTextStyle: .headline)
        label.font = UIFont.systemFont(ofSize: font.pointSize,weight: UIFont.Weight.bold)
        label.textAlignment = .center
        label.textColor = .white
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    private func commonInit() {
        addSubview(circleView)
        addSubview(countLabel)

//***** START - UPDATED BASED ON SUGGESTION IN COMMENTS******
countLabel.translatesAutoresizingMaskIntoConstraints = false
//***** END - UPDATED BASED ON SUGGESTION IN COMMENTS******
    countLabel.centerXAnchor.constraint(equalTo: circleView.centerXAnchor).isActive = true
    countLabel.centerYAnchor.constraint(equalTo: circleView.centerYAnchor).isActive = true   
            }
        }

馆藏视图单元格

var img = UIImageView()
    var overlayView = UIView()
    
    var asset: PHAsset? {
        didSet {}
    }
    
    var isVideo: Bool = false {
        didSet {
            durationLabel.isHidden = !isVideo
        }
    }
    
    override var isSelected: Bool {
        didSet { overlay.isHidden = !isSelected }
    }
    
    var imageView: UIImageView = {
        let view = UIImageView()
        view.clipsToBounds = true
        view.contentMode = .scaleAspectFill
        view.backgroundColor = UIColor.gray
        return view
    }()
    
    var count: Int = 0 {
        didSet { overlay.countLabel.text = "\(count)" }
    }
    
    var duration: TimeInterval = 0 {
        didSet {
            let hour = Int(duration / 3600)
            let min = Int((duration / 60).truncatingRemainder(dividingBy: 60))
            let sec = Int(duration.truncatingRemainder(dividingBy: 60))
            var durationString = hour > 0 ? "\(hour)" : ""
            durationString.append(min > 0 ? "\(min):" : ":")
            durationString.append(String(format: "%02d",sec))
            durationLabel.text = durationString
        }
    }
    
    let overlay: CustomAssetCellOverlay = {
        let view = CustomAssetCellOverlay()
        view.isHidden = true
        return view
    }()
    
    let durationLabel: UILabel = {
        let label = UILabel()
        label.preferredMaxLayoutWidth = 80
        label.backgroundColor = .gray
        label.textColor = .white
        label.textAlignment = .right
        label.font = UIFont.boldSystemFont(ofSize: 20)
        return label
    }()
    
    func commonInit() {      
        addSubview(imageView)
        imageView.addSubview(overlay)
        imageView.addSubview(durationLabel)
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        

//***** START - UPDATED BASED ON SUGGESTION IN COMMENTS******
    overlay.translatesAutoresizingMaskIntoConstraints = false
    overlayView.translatesAutoresizingMaskIntoConstraints = false
//***** END - UPDATED BASED ON SUGGESTION IN COMMENTS******

        NSLayoutConstraint.activate([
            overlay.topAnchor.constraint(equalTo: imageView.topAnchor),overlay.bottomAnchor.constraint(equalTo: imageView.bottomAnchor),overlay.leftAnchor.constraint(equalTo: imageView.leftAnchor),overlay.rightAnchor.constraint(equalTo: imageView.rightAnchor),overlayView.centerXAnchor.constraint(equalTo: overlay.centerXAnchor),overlayView.centerYAnchor.constraint(equalTo: overlay.centerYAnchor),overlayView.widthAnchor.constraint(equalToConstant: 80.0),overlayView.heightAnchor.constraint(equalToConstant: 80.0),]
        )
    }
//Some other stuff

example of image overlay

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)