2

I am sizing a NSCollectionLayoutItem in its group. Using NSCollectionLayoutSize and NSCollectionLayoutDimension I can achieve requirements such as ‘I want it to be half the width of its container’ or ' I want it to be 200px’ or ’I want its height to be the same as its width plus 10%’.

But I can’t find a way to implement what I need: ‘I want its height to be the same as its width plus 28px’.

Is that possible at all?

Code:

private lazy var myFancyLayout: NSCollectionLayoutSection = {
        let itemSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .fractionalWidth(1.0)
        )
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        
        // Make the group with width equal to half container width and with height 110% its width
        let groupSize_fractional = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(0.50),
            heightDimension: .fractionalWidth(0.50 * 1.1)
        )
        
        // Make the group with width equal to 100px and with height 110% its width
        let groupSize_absoulte = NSCollectionLayoutSize(
            widthDimension: .absolute(100),
            heightDimension: .absolute(100 * 1.1)
        )
        
        // Make the group with width equal to half container width and with height same as its width plus 20px
        let groupSize_mixed_impossibleToImplement = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(0.50),
            heightDimension: .fractionalWidth(0.50) // + 20px ??
        )
        
        let group = NSCollectionLayoutGroup.horizontal(
            layoutSize: groupSize_fractional, // or groupSize_absoulte or groupSize_mixed_impossibleToImplement
            subitem: item,
            count: 1
        )

        let section = NSCollectionLayoutSection(group: group)
        
        // ... further configuration
        
        return section
}()
user236739
  • 1,323
  • 2
  • 15
  • 21
  • I don't think so. This would make a reasonable feature request to Apple. – matt Sep 30 '21 at 15:06
  • Can you show the code you are getting stuck in. This should be possible as long as you are able to express the sizes even in an equation to define what you want. But we would need to know where you are starting from with your code – Fogmeister Sep 30 '21 at 15:14
  • Yes, sure, thanks @Fogmeister, added code to question. – user236739 Oct 04 '21 at 15:58

1 Answers1

0

You can compute the fractional values yourself, add a constant and give it to the Layout object as absolute.

let fractionalWidth = UIScreen.main.bounds.width/numberOfItems - trailinginsets - leadinginsets
let desiredHeight = fractionalHeight + 20
...
heightDimension = .absolute (desiredHeight)