2016-12-02 138 views
1

我試圖調整我的collectionViewCells每次我旋轉設備,但我不能弄清楚如何。這是我的問題和我的代碼。這是一個在夫特3和的Xcode 8.如何在設備旋轉時自動調整UICollectionView的大小?

這是我們加載 1

並在第一屏幕時,我旋轉器件單元的仍具有相同的寬度,高度 2

在另一方面,如果我當設備旋轉(橫向)時加載firstScreen(HomePage),然後再次將設備旋轉到Portrait,單元格溢出,所以我不能看到它們的側面。因爲寬度仍然與橫向寬度相同。

// 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中使用Auto-Layout。這裏是HomeCollectionViewCell類

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

問候我setupViews()方法。

解決!:我用這個方法,而不是覆蓋FUNC didRotate(從fromInterfaceOrientation:UIInterfaceOrientation){}

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

您是否在使用自動佈局形式故事板或筆尖? –

+0

感謝您的解決方案,這對我有用 - swift 3 xcode 8.3.1部署目標iOS 10.0 – Jeeves

回答

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

這適用於我。嘗試一下。

+0

謝謝我實際上已經在您的幫助下解決了它。唯一的區別是我沒有在viewWillTransitition方法中使用特定的itemSize寬度/高度尺寸。因爲invalidateLayout()方法爲我們處理。 –

+0

@UnalCelik是的。但有時候,尺寸差異很大,我們必須調整它的大小。 –

1

您應該使用self.view.bounds代替self.view.frame因爲當設備旋轉的幀在縱向和橫向上僅保留相同的更改。

0

因爲我在集合視圖中有固定的行數,所以當設備旋轉時我必須調整我的單元大小。我希望這幫助:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
    guard let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout else { 
     return 
    } 

    if UIInterfaceOrientationIsLandscape(UIApplication.shared.statusBarOrientation) { 
     //here you can do the logic for the cell size if phone is in landscape 

    } else { 
     //logic if not landscape 
    } 
    let cellSize = CGSize(width: (size.width - CGFloat(row))/CGFloat(row) , height: 90) 
    flowLayout.itemSize = cellSize 
    flowLayout.invalidateLayout() 
} 

* 是我的行數,你可以改變它適合你。

相關問題