ios - collectionView 在标题中隐藏我的自定义 View

标签 ios swift collectionview

我正在尝试将 customView 添加到我的 collectionViewheaderView 中。这是我的代码:

我在viewDidLoad中注册了headerView类:

collectionView.register(SearchReuseView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "reuseCell")

然后我设置collectionView:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    var reuseView: UICollectionReusableView?
    if indexPath.section == 0 || banners.count == 0 {
        let view = UICollectionReusableView(frame: CGRect.zero)
        reuseView = view
    } else {
        if kind == UICollectionElementKindSectionHeader {
            let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "reuseCell", for: indexPath) as! SearchReuseView
            view.backgroundColor = .black
            view.delegate = self
            view.frame = CGRect(x: 0, y: 0, width: collectionView.frame.width, height: 100)
            view.item = banners[indexPath.section % banners.count]
            return view
        }
    }
    return reuseView!
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if section == 0 {
        return CGSize.zero
    }
    return CGSize(width: collectionView.frame.width, height: 100)
}

但是当我在 Simulator 上运行时,collectionView 显示标题但内部什么也没有。

这是 SearchReuseView 的代码:

class SearchReuseView: UICollectionReusableView {

weak var delegate: SearchReuseDelegate?

let adImageView: UIImageView = {
    let img = UIImageView()
    img.translatesAutoresizingMaskIntoConstraints = false
    img.contentMode = .scaleAspectFit
    return img
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    self.setupViews()
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    self.setupViews()
}

func setupViews() {
    backgroundColor = .black
    adImageView.isUserInteractionEnabled = true
    adImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: self.adImageView, attribute: .bottom, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .right, relatedBy: .equal, toItem: self.adImageView, attribute: .right, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: self.adImageView, attribute: .top, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .left, relatedBy: .equal, toItem: self.adImageView, attribute: .left, multiplier: 1, constant: 0))
    addSubview(adImageView)
}

var item: Banner! {
    didSet {
        guard let img = item.img else {return}
        let url = URL(string: "\(img)")
        self.adImageView.kf.setImage(with: url, placeholder: nil, options: [.transition(ImageTransition.fade(1))], progressBlock: { (receivedSize, totalSize) in
            }) { (image, error, cacheType, imageURL) in
            if error != nil {}
        }
    }
}

@objc func handleTap(){
    guard let link = item.link else {
        return
    }
    delegate?.didTapAds(link: link)
}
}

最佳答案

我想知道为什么你的应用没有崩溃你应该先添加 subview 然后再添加约束

用下面的代码替换你的代码

func setupViews() {
    backgroundColor = .black
    addSubview(adImageView)
    addSubview.translatesAutoresizingMaskIntoConstraints = false

    adImageView.isUserInteractionEnabled = true
    adImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: self.adImageView, attribute: .bottom, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .right, relatedBy: .equal, toItem: self.adImageView, attribute: .right, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: self.adImageView, attribute: .top, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .left, relatedBy: .equal, toItem: self.adImageView, attribute: .left, multiplier: 1, constant: 0))
}

希望对你有帮助

关于ios - collectionView 在标题中隐藏我的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245433/

相关文章:

iOS 构建使用 Codemagic 失败 [适用于 Xcode]

iOS NSURLConnection 错误-1002

ios - 是否可以在 collectionview sizeForItemAtIndexPath 委托(delegate)方法中获取单元格的标签?

ios - 约束 UIScrollView 平移?

ios - "Apple Music API"的定义是什么?

ios - 如何从代码中以编程方式设置 uiimageview 的大小以在我的 swift 应用程序中覆盖全屏?

ios - CollectionViewCell 未创建

ios - 在导航单元格时将 collectionView 单元格中的 textView 设为第一个响应者

ios - Objective-C 中带下划线前缀的变量名是什么意思?

swift - 为集合中的每个项目添加一个按钮