UICollectionViewFlowLayout问题

时间:2020-02-06 09:41:15

标签: ios swift uicollectionview

提前道歉,但我已经为此圈了好几天...

我有一个UIViewController,是从另一个VC呈现的(在我的情况下,点击了一个按钮)。新的VC(下面的代码)由以下组成:

  1. 一个UITextView,它会根据用户键入的内容动态增加高度
  2. 高度固定的UIView

,然后在其下方,是ViewController视图的底部,是一个UICollectionView。其中有5个部分可以水平滚动。我将在这5个单元格中的每个单元格中插入不同的内容,但是在UICollectionView调整大小时,这只是个问题。除了一个,我已经能够清除其中的大多数。

只要UITextView的大小增加到3-6行之间,然后又增加到13行,就可以重现此错误。这在模拟器中会发生,例如使用iPhone 8 plus,但在iPhone 11 Pro Max上,它仅发生在UITextView中大约19行文本中。

我在这里使用TinyContraints,但是我已经使用传统的程序约束进行了测试,并且存在相同的问题。

//
//  NewItemVC.swift
//

import TinyConstraints
import UIKit

class NewItemVC: UIViewController {

    let titleField = UITextView()
    let bannerContainer = UIView(frame: .zero)
    var flowLayout = UICollectionViewFlowLayout()
    lazy var horizontalCV = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)

    let genericCellID = "genericCellID"


    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .orange

        setupHeaderArea()
        setupBannerArea()
        setupHorizontalCollectionView()
    }



    func setupHeaderArea(){

        titleField.backgroundColor = .lightGray

        titleField.textContainerInset.top = 55
        titleField.textContainerInset.bottom = 20
        titleField.textContainerInset.left = 15
        titleField.textContainerInset.right = 15
        titleField.font = UIFont.boldSystemFont(ofSize: 20)

        // add to main view & position
        view.addSubview(titleField)
        titleField.topToSuperview()
        titleField.leftToSuperview()
        titleField.rightToSuperview()

        titleField.isScrollEnabled = false

        NotificationCenter.default.addObserver(self, selector: #selector(updateTextFieldHeight), name: UITextView.textDidChangeNotification, object: nil)

        // add a header label
        let headerLabel = UILabel()
        headerLabel.textColor = .systemBlue
        headerLabel.text = "NEW ITEM"
        headerLabel.font = UIFont.systemFont(ofSize: 12)

        view.addSubview(headerLabel)
        headerLabel.topToSuperview(offset: 35)
        headerLabel.leftToSuperview(offset: 20)
        headerLabel.height(13)

        // add card handle
        let handle = UIView()
        handle.backgroundColor = .black
        handle.alpha = 0.2
        handle.width(45)
        handle.height(5)
        handle.layer.cornerRadius = 5/2

        view.addSubview(handle)
        handle.topToSuperview(offset: 10)
        handle.centerXToSuperview()

    }



    func setupBannerArea(){

        // position container
        view.addSubview(bannerContainer)
        bannerContainer.topToBottom(of: titleField)
        bannerContainer.edgesToSuperview(excluding: [.top, .bottom])
        bannerContainer.height(62)
        bannerContainer.backgroundColor = .cyan

    }



    func setupHorizontalCollectionView() {

        horizontalCV = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        horizontalCV.dataSource = self
        horizontalCV.delegate = self

        horizontalCV.backgroundColor = .yellow
        horizontalCV.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        horizontalCV.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        view.addSubview(horizontalCV)
        horizontalCV.topToBottom(of: bannerContainer)
        horizontalCV.edgesToSuperview(excluding: .top)

        horizontalCV.isPagingEnabled = true
        horizontalCV.contentInsetAdjustmentBehavior = .never

        flowLayout.scrollDirection = .horizontal
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0

        horizontalCV.register(UICollectionViewCell.self, forCellWithReuseIdentifier: genericCellID)

    }



    @objc func updateTextFieldHeight() {
        DispatchQueue.main.async{
            self.horizontalCV.collectionViewLayout.invalidateLayout()
        }
    }


}


// MARK:- Delegates - UITextViewDelegate

extension NewItemVC: UITextViewDelegate {

    func textViewDidChange(_ textView: UITextView) {
        let size = CGSize(width: textView.frame.width, height: .infinity)
        let estimatedSize = textView.sizeThatFits(size)

        textView.constraints.forEach { (constraint) in

            if constraint.firstAttribute == .height {
                constraint.constant = estimatedSize.height
            }
        }
    }

}



// MARK:- UICollectionView Data Source

extension NewItemVC: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }



    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: genericCellID, for: indexPath)
        if indexPath.row % 2 == 0 {
            cell.backgroundColor = .orange
        } else {
            cell.backgroundColor = .red
        }
        return cell
    }

}



// MARK:- Delegates - UICollectionView Flow Layout

extension NewItemVC: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let requiredHeight = self.view.frame.height - (titleField.frame.height + bannerContainer.frame.height)
        let requiredWidth = view.frame.width

        return CGSize(width: requiredWidth, height: requiredHeight)
    }

}


据我所知,随机存在一些问题,其中collectionView框架和contentSize超出了24点,但是我无法弄清楚为什么/在哪里等等...

错误如下:

2020-02-06 20:39:18.425280+1100 Scrolling[67237:8005204] The behavior of the UICollectionViewFlowLayout is not defined because:
2020-02-06 20:39:18.425378+1100 Scrolling[67237:8005204] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2020-02-06 20:39:18.425672+1100 Scrolling[67237:8005204] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fcf56d4a120>, and it is attached to <UICollectionView: 0x7fcf5785d000; frame = (0 208.667; 414 487.333); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000ce8a50>; layer = <CALayer: 0x600000213a00>; contentOffset: {0, 0}; contentSize: {2070, 511}; adjustedContentInset: {0, 0, 0, 0}; layout: <UICollectionViewFlowLayout: 0x7fcf56d4a120>; dataSource: <Scrolling.NewItemVC: 0x7fcf56d20110>>.
2020-02-06 20:39:18.425760+1100 Scrolling[67237:8005204] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

0 个答案:

没有答案