UICollectionView仅针对电话

时间:2015-12-05 19:01:09

标签: ios iphone swift storyboard uicollectionview

我有一个UICollectionView,每个部分使用一个单元格。它垂直滚动。我已经将集合视图单元格大小设置为320X125,尽管我玩宽度无济于事。

当在纵向模式下在iPhone上时,单元格从屏幕左侧开始,这是所需的行为。但是,当我将手机旋转到横向时,单元格会在集合视图中居中。然而,在iPad上,它在两个方向都保持在左侧。

我希望无论方向如何,单元格都会粘在屏幕的左侧。如何实现这一目标?虽然自动布局适用于单元内部的事物,但似乎没有可以应用于单元本身的自动布局。

1 个答案:

答案 0 :(得分:0)

我通过为iPad创建第二个单元解决了这个问题。所以有一款适用于iPhone和另一款iPad。然后我用了:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

根据设备方向,我设置了preferredMaxLayoutWidth我使用的时间:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
        let deviceType = UIDevice.currentDevice().userInterfaceIdiom

        if deviceType == .Phone
        {
            return CGSizeMake(self.view.bounds.width, 170)
        }
        else
        {
            return CGSizeMake(self.view.bounds.width, 400)
        }
    }

根据手机或平板电脑调整了手机的大小。

作为最后一项,我在旋转设备后使用以下内容重新绘制:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
    {
        coordinator.animateAlongsideTransition(nil) { (context) -> Void in
            self.collectionsView.reloadData()
        }
    }