如何在设备旋转时自动调整UICollectionView的大小?

时间:2016-12-02 08:21:00

标签: swift mobile xcode8

我每次旋转设备时都试图调整我的collectionViewCells的大小,但我无法弄清楚如何。这是我的问题和我的代码。它位于Swift 3和Xcode 8中。

这是我们加载的第一个屏幕 1

当我旋转设备单元时,仍然具有相同的宽度 - 高度 2

另一方面,如果我在旋转设备(横向)时加载firstScreen(HomePage)然后再将设备再次旋转到纵向,则单元格会溢出,因此我无法看到它们的两侧。因为宽度与横向宽度仍然相同。

    // MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return allNews.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! HomeCollectionViewCell

    // Configure the cell
    cell.imageView.image = allNews[indexPath.row].newsPic
    cell.textView.text = allNews[indexPath.row].newsTitle
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let height = (view.frame.width - 32) * 9 / 16
    return CGSize(width: view.frame.width, height: height + 94)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}


// MARK: Orientation Changes ( reload the CollectionLayout )
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {

}

最后,我没有在Main.storyboard中使用自动布局。这是HomeCollectionViewCell类中的setupViews()方法

    private func setupViews() {
    addSubview(imageView)
    addSubview(textView)
    addSubview(separatorView)
    addSubview(dateView)

    addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: imageView)
    addConstraintsWithFormat(format: "V:|-16-[v0]-8-[v1(40)]-2-[v2(20)]-8-|", views: imageView, textView, dateView)

    addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: textView)
    addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: dateView)

    addConstraintsWithFormat(format: "H:|[v0]|", views: separatorView)
    addConstraintsWithFormat(format: "V:[v0(1)]|", views: separatorView)
}

问候。

已解决!:我使用此方法代替覆盖func didRotate(来自fromInterfaceOrientation:UIInterfaceOrientation){}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    collectionView?.collectionViewLayout.invalidateLayout()
}

3 个答案:

答案 0 :(得分:1)

您应该使用self.view.bounds而不是self.view.frame,因为当设备旋转的框架在纵向和横向仅保留绑定更改时保持相同。

答案 1 :(得分:1)

查看我的解决方案。

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 100, height: 100)
let controller = UICollectionViewController(collectionViewLayout: layout))

在iOS8 +中,该功能不再是override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        if let layout =  self.collectionView?.collectionViewLayout as? UICollectionViewFlowLayout{
            layout.itemSize = CGSize(width: 200, height: 200)
        }
    }

这对我有用。试试吧。

答案 2 :(得分:0)

因为我在集合视图中有固定的行数,所以我必须在设备旋转时调整单元格大小。我希望这有帮助:

find

* 是我的行数,您可以更改它适合您。

相关问题