Invalid absolute dimension: inf. The dimension must be a finite value

127 Views Asked by At

Hi I tried to use compositional layout, while using stage manager rare the app getting crashed. Please refer below error message

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid absolute dimension: inf. The dimension must be a finite value.'

code

let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(cellWidth),heightDimension: .absolute(cellHeight))
            let item = NSCollectionLayoutItem(layoutSize: itemSize)
            
            let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(cellHeight))
            let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
            group.interItemSpacing = NSCollectionLayoutSpacing.fixed(cellSpacing)
            
            let section = NSCollectionLayoutSection(group: group)
            section.contentInsets = NSDirectionalEdgeInsets(top: 10,leading: inset, bottom: 0,trailing:inset)
            section.interGroupSpacing = lineSpacing
1

There are 1 best solutions below

0
Mykyta iOS On
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(cellWidth),heightDimension: .absolute(cellHeight))

In case this line produces exception due to infinite parameter, that may happen due to division by zero when calculating cellWidth or cellHeight. I can assume you're diving total width of collection component by count of elements, or similar. Avoid division by zero issues by adding security checks for this edge case.