【问题标题】:CollectionView is not showing inside a view in SwiftCollectionView 未显示在 Swift 的视图中
【发布时间】:2021-07-16 10:57:59
【问题描述】:

我有一个视图,里面有一个集合视图。我将此视图添加到另一个视图,但未显示集合视图。我在这里做错了什么?

我有一个包含集合视图的视图

class GiftFilterView: UIView {
    
    var giftImageIcon: [UIImage] = [#imageLiteral(resourceName: "Gift Mic Icon"), #imageLiteral(resourceName: "Gift Smile Icon"), #imageLiteral(resourceName: "Gift Gif Icon"), #imageLiteral(resourceName: "Gift Camera Icon"), #imageLiteral(resourceName: "Gift Gift Icon"), #imageLiteral(resourceName: "Gift Three Dot Icon")]

    //MARK:- Properties
    weak var delegate: sendMessageToChatScreenDelegate?
    weak var giftIcondelegate: GiftIconCellActionDeleate?
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.backgroundColor = .white
        cv.delegate = self
        cv.dataSource = self
        cv.isScrollEnabled = false
        return cv
    }()
     lazy var customInputView: CustomInputAccesoryView = {
        let customView = CustomInputAccesoryView()
        customView.delegate = self
        return customView
    }()

     //MARK:- Lifecycles
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(customInputView)
        customInputView.anchor(top: topAnchor, left: leftAnchor, right: rightAnchor, paddingTop: 5)
        addSubview(collectionView)
        collectionView.anchor(top: customInputView.bottomAnchor, left: leftAnchor, paddingTop: 10)
        collectionView.setDimensions(width: self.frame.size.width, height: 30)
        backgroundColor = .white
        self.collectionView.register(GiftFilterCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK:- UICollectionViewDataSource, UICollectionViewDelegate
extension GiftFilterView: UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return giftImageIcon.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? GiftFilterCollectionViewCell else {
            return UICollectionViewCell()
        }
        cell.imageIcon = giftImageIcon[indexPath.row]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        giftIcondelegate?.changeKeyboardheight()
    }
    
}
//MARK:- UICollectionViewDelegateFlowLayout
extension GiftFilterView: UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: frame.width / CGFloat(giftImageIcon.count), height: 40)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
}

我正在将此视图添加到另一个视图中


class GiftAccessoryCollectionView: UIView {
  

    //MARK:- Properties
    weak var delegate: sendMessageToChatScreenDelegate?
    weak var giftIcondelegate: GiftIconCellActionDeleate?
    
    private lazy var filterView: GiftFilterView = {
        let giftView = GiftFilterView()
        giftView.translatesAutoresizingMaskIntoConstraints = false
        return giftView
    }()
    
    //MARK:- Lifecycles
    override init(frame: CGRect) {
        super.init(frame: frame)
       addSubview(filterView)
        filterView.anchor(top: topAnchor, left: leftAnchor, right: rightAnchor,height: 130)
        addSubview(collectionView)
        collectionView.anchor(top: filterView.bottomAnchor, left: leftAnchor, paddingTop: 10)
        collectionView.setDimensions(width: self.frame.size.width, height: 260)

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

但是当我将视图添加到另一个视图时,集合视图没有显示。

【问题讨论】:

    标签: swift collectionview


    【解决方案1】:

    看起来您需要使用isActive 属性或多个视图来激活锚点,以下会更有用:

    NSLayoutConstraint.activate([
        ...
    ])
    

    【讨论】:

      猜你喜欢
      • 2021-03-08
      • 1970-01-01
      • 2019-02-22
      • 2021-10-13
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多