在所有屏幕尺寸的CollectionView中正确显示Instagram照片

时间:2015-02-24 02:58:52

标签: ios swift uicollectionview instagram-api

这个让我感到困惑。好的,我们走了。 我正在拉下IG图像,它们有3种尺寸: 150x150,306x306,640x640

我想要做的是在所有iPhone屏幕尺寸(5,5S,6,6 +)上显示3个。 306x306尺寸超快速下降,看起来很棒。所以这是我的起点(150x150有点模糊,640x640也是一个选项,我也可以使用它们,只是试图节省一些带宽)。

边缘可以与侧面齐平,它们之间有1 px / pt线。我有点像这样缠绕着我的大脑。我的理解是,函数(下面)应该覆盖我的Storyboard中的任何其他设置,以及我应该关注的位置(我已经打印出我从println()中捕获的大小。

我的挑战是找出在所有屏幕上工作所需的最终宽度和高度。我已经尝试了一些排列,但还没有固定它。我很近,但没有雪茄。这都是在故事板中使用CollectionView。建议表示赞赏。这是一个问题,整天花在这一个问题上。

我认为autolayout根本不能提供帮助,这一切都必须在代码中完成,但是谁知道呢?

谢谢[lot!]: - )

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    var width = CGRectGetWidth(collectionView.bounds)

    println("width: \(width)")

    // iphone5 320
    // iphone5S 320
    // iphone6 375
    // iphone 6+ 414

    return CGSize(width: NeedCorrectWidthHere, height: NeedCorrectHeightHere)

}

这部分有效,图像之间存在差距:

var screenWidth = CGRectGetWidth(collectionView.bounds)
var cellWidth = screenWidth/3

return CGSize(width: cellWidth-10, height: cellWidth-10)

1 个答案:

答案 0 :(得分:2)

这似乎有效。一个非常简单的解决方案到底。

   func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    var screenWidth = CGRectGetWidth(collectionView.bounds)
    var cellWidth = screenWidth/3

    return CGSize(width: cellWidth-2, height: cellWidth-2)

}

    func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
            return 3
    }

    func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
            return 3
    }