2016-12-02 8 views
1

私はデバイスを回転させるたびに私のcollectionViewCellsのサイズを変更しようとしていますが、どのようにそれを把握できません。ここに私の問題と私のコードです。これは、スウィフト3とXcodeでだ8.デバイスの回転時にUICollectionViewを自動サイズ変更する方法は?

それは我々が 1

をロードし、最初の画面です私は、デバイス一方、セルのまだ持って同じ幅、高さ 2

、場合回転したときに、私デバイスが回転しているときにfirstScreen(HomePage)をロードしてから、デバイスをPortraitにもう一度回転させると、セルがオーバーフローするので、それらの側面が見えなくなります。横幅は横幅と同じですから。

最後に、私はMain.storyboardで自動レイアウトを使用しませんでした。ここで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()メソッドです。

を解決!: Iは(fromInterfaceOrientationから:UIInterfaceOrientation)didRotate FUNCこの方法の代わりに、オーバーライドを使用

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

ストーリーボードまたはペン先の自動レイアウトを使用していますか? –

+0

ソリューションをありがとう、これは私のために働いた - 迅速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メソッドでspesific itemSizeの幅/高さのサイズを使用しなかったことだけです。 invalidateLayout()メソッドが私たちのためにそれを処理するためです。 –

+0

@UnalCelikサイズが大きく異なる場合があります。サイズを変更する必要があります。 –

1

} {かわりself.view.frameのself.view.boundsを使用する必要があるときためデバイスの回転したフレームは、ポートレートとランドスケープの両方の変更で同じままです。

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

* はあなたに適して変更することができ、行の私の電話番号です。

関連する問題