如何获取当前的UIEdgeInsetsMake?

How to get the curent UIEdgeInsetsMake?

我已经通过故事板创建了集合视图单元格,对于 4 英寸和 5 英寸的屏幕,我以编程方式设置了 UIEdgeInsetsMake,如果设备不是 4 和 5,那么我的故事板 UIEdgeInsetsMake 必须为此设置。

怎么做?

这是我的代码

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                        layout:(UICollectionViewLayout*)collectionViewLayout
        insetForSectionAtIndex:(NSInteger)section {
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;
    UIEdgeInsets insets= UIEdgeInsetsMake(?,?,?,?);//how to get the current edge insets.
    if(screenWidth==320)
    {
        UIEdgeInsets insets = UIEdgeInsetsMake(0,12,20,2);
        return insets;
    }
        return insets;
}

您正在寻找 contentInset 属性。

此外,您在代码中重新声明了变量 insets。为简单起见,考虑更改为以下内容:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout*)collectionViewLayout
    insetForSectionAtIndex:(NSInteger)section {

    if([UIScreen mainScreen].bounds.size.width == 320)
    {
        return UIEdgeInsetsMake(0,12,20,2);
    }

    return collectionView.contentInset;

}