【问题标题】:collectionView hide my custom view in headercollectionView 在标题中隐藏我的自定义视图
【发布时间】:2018-05-09 04:16:50
【问题描述】:

我正在尝试将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)
}

但是当我在模拟器上运行时,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)
}
}

【问题讨论】:

  • 你隐藏第一节标题吗?
  • 是的,我愿意。我只想显示第二部分的标题

标签: ios swift collectionview


【解决方案1】:

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

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

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))
}

希望对你有帮助

【讨论】:

    【解决方案2】:
        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) //delete this line
            view.item = banners[indexPath.section % banners.count]
            return view
        }
    

    我删除了标题视图的线集框架,它的工作原理

    【讨论】:

      猜你喜欢
      • 2017-05-14
      • 2022-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多