UICollectionView предупреждение - PullRequest
       54

UICollectionView предупреждение

0 голосов
/ 04 октября 2018

Предупреждение: The item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.

Код:

extension SaleViewController {
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
        checkEstimatedSize()
    }

    func checkEstimatedSize(){
        if(DeviceType.IS_IPAD || UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight){
            if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
                layout.estimatedItemSize = CGSize(width: 1, height: 1)
                layout.invalidateLayout()
            }
        }else{
            if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
                layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
                layout.invalidateLayout()
            }
        }
    }
}

Я не уверен, в чем проблема ... он имеет UICollectionViewFlowLayoutAutomaticSize и как wrap_content на iPad,Ошибка звучит как UICollectionViewFlowLayoutAutomaticSize;это работает иногда.Когда это происходит, работает отлично.Когда этого не происходит, он замораживает и заполняет журнал следующим образом:

The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x10313a9a0>, and it is attached to <UICollectionView: 0x104096e00; frame = (10 138; 300 420); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x1c0458750>; layer = <CALayer: 0x1c02254e0>; contentOffset: {0, 0}; contentSize: {300, 220}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x10313a9a0>. 2018-10-04 11:03:35.869371-0500 MyApp[276:5353] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger. 2018-10-04 11:03:35.869398-0500 MyApp[276:5353] The behavior of the UICollectionViewFlowLayout is not defined because: 2018-10-04 11:03:35.869445-0500 MyApp[276:5353] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values. 2018-10-04 11:03:35.869555-0500 MyApp[276:5353] Please check the values returned by the delegate.

И в основном он повторяет один и тот же журнал снова и снова.

СпасибоВы заранее.

1 Ответ

0 голосов
/ 20 ноября 2018

Ваша проблема возникает из-за того, что ссылка width больше, чем "рабочая" ширина вашей collectionView

. Чтобы исправить это, вы должны правильно рассчитать эталонный размер ваших ячеек.

Вы используете UICollectionViewFlowLayout, поэтому вы можете воспользоваться преимуществом UICollectionViewDelegateFlowLayout и реализовать следующий метод (он предусмотрен для случая, когда ячейки имеют полноэкранный режим width и динамическую высоту):

        /// UICollectionViewDelegateFlowLayout
        func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            sizeForItemAt indexPath: IndexPath) -> CGSize {
            var referenceHeight: CGFloat = 54.0 // Approximate height of the cell
            // Cell width calculation
            let sectionInset = (collectionViewLayout as! UICollectionViewFlowLayout).sectionInset
            let referenceWidth = collectionView.safeAreaLayoutGuide.layoutFrame.width
                - sectionInset.left
                - sectionInset.right
                - collectionView.contentInset.left
                - collectionView.contentInset.right

                return CGSize(width: referenceWidth, height: referenceHeight)
            }
        }

Имейте в виду, что если вы используете несколько столбцов, вы должны рассчитать ячейки width соответственно и minInteritemSpacing должны быть приняты во внимание.Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...