xcode 混合使用分数值和绝对值来调整NSCollectionLayoutItem的大小

5w9g7ksd  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(128)

我正在调整组中NSCollectionLayoutItem的大小。使用NSCollectionLayoutSizeNSCollectionLayoutDimension可以满足诸如“* 希望它是其容器宽度的一半 ”或“ 希望它是200px*”或“* 希望它的高度等于其宽度加10%”之类的要求。
但我找不到一种方法来实现我所需要的:'
我希望它的高度等于宽度加上28px *'。
这可能吗?
代码:

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
}()
r6hnlfcb

r6hnlfcb1#

您可以自己计算分数值,添加一个常数并将其作为绝对值赋予Layout对象。

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

相关问题