UICollectionViewCell大小更大的UICollectionView大小

时间:2017-01-20 17:20:55

标签: swift uicollectionview cell

我遇到以下运行时错误:项目高度必须小于UICollectionView的高度减去截面插入顶部和底部值,减去内容插入顶部和底部值。

这在实现后开始出现,将我的自定义UICollectionViewCell调整为UISearchBar项。当我点击它时,键盘显示出来,并且单元格被半滑到顶部。

这些是我的相关代码:

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

 func setupCollectionView() {
        if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.scrollDirection = .horizontal
            flowLayout.minimumLineSpacing = 0
            flowLayout.invalidateLayout()

        }

        collectionView?.backgroundColor = UIColor.white
        collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: cellId)
        collectionView?.register(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId)
        collectionView?.register(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId)
        collectionView?.register(MapCell.self, forCellWithReuseIdentifier: mapCellId)
        collectionView?.register(AccountCell.self, forCellWithReuseIdentifier: accountCellId)


        collectionView?.contentInset = UIEdgeInsetsMake(0, 0, 50, 0)
        collectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 50, 0)

        collectionView?.isPagingEnabled = true

        view.addSubview(menuBar)
        view.addConstraintsWithFormat(format: "H:|[v0]|", views: menuBar)
        view.addConstraintsWithFormat(format: "V:[v0(50)]", views: menuBar)

        menuBar.bottomAnchor.constraint(equalTo:  (collectionView?.bottomAnchor)!).isActive = true


    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height - 50)
    }

}

class MapCell:  UICollectionViewCell{

    let topContainer: TopContainerView = {
        let v = TopContainerView()

        return v
    }()


    override func setupViews() {
        super.setupViews()

        backgroundColor = .brown

        topContainer.setupView()
        addSubview(topContainer)

        addConstraintsWithFormat(format: "V:[v0(40)]", views: topContainer)
        addConstraintsWithFormat(format: "H:|[v0]|", views: topContainer)

        addConstraint(NSLayoutConstraint(item: topContainer, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: topContainer, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0))
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.endEditing(true)

    }}

class TopContainerView: UIView {

    func setupView(){
        backgroundColor = .yellow


        addSubview(searchField)
        addConstraintsWithFormat(format: "V:|[v0]|", views: searchField)
        addConstraintsWithFormat(format: "H:|[v0]-40-|", views: searchField)

        addConstraint(NSLayoutConstraint(item: searchField, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: searchField, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 5))
    }

    let searchField: UISearchBar = {
        let sf = UISearchBar()
        sf.placeholder = "Search"

        return sf
    }()

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.endEditing(true)

    }}

有人有任何想法吗?

0 个答案:

没有答案