2016-11-05 12 views
0

私はViewControllerに非常に単純なUICollectionViewを埋め込んでいます。UICollectionViewCellはデバイスの回転でサイズ変更を行います

UICollectionViewは、自動レイアウト(IBで定義)を使用して、ViewController内の全領域を占有します。

私のUICollectionViewCellはすべて同じサイズを持ち、ViewControllerに1つのセルしか表示されないようにするため、UICollectionView全体の領域を占めます。 UICollectionViewCellはUIImage(ズーム用のUIScrollViewに埋め込まれている)のみを表示します。

デバイスが回転する以外は正常に動作します。

のでUICollectionViewFlowLayoutの動作が定義されていない:アイテムの高さがUICollectionViewの高さよりも小さくなければなりません マイナスセクションのトップインセット値とボトム値、マイナスコンテンツのインセットは、上部と下部のエラーメッセージがあります値。 関連するUICollectionViewFlowLayoutインスタンスがあり、それが添付されています。アニメーション= {bounds.origin =; bounds.size =;位置=; };層=; contentOffset:{0、0}; contentSize:{4875、323}>コレクションビューレイアウト:。

これはかなり明確ですが、新しい高さ/幅に合わせてUICollectionViewCellのサイズを変更する必要があります。回転後、UICollectionViewCellは適切な高さ/幅で表示されますが、アニメーションは不十分です。

ここでは、コードです:

@IBOutlet weak var theCollectionView: UICollectionView! 
@IBOutlet weak var theFlowLayout: UICollectionViewFlowLayout! 
    override func viewDidLoad() { 
    super.viewDidLoad() 

    //theCollectionView.delegate = self 
    theCollectionView.dataSource = self 
    automaticallyAdjustsScrollViewInsets = false 
    theFlowLayout.itemSize = theCollectionView.frame.size 
    theFlowLayout.sectionInset = UIEdgeInsetsMake(0,0,0,0) 
    theFlowLayout.minimumLineSpacing = 0.0 
    theCollectionView.reloadData() 
} 


override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
} 

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
    super.viewWillTransition(to: size, with: coordinator) 
    print("\(#function) \(view.frame.size)") 
    coordinator.animate(alongsideTransition: nil, completion: { 
     _ in 
     self.theFlowLayout.itemSize = self.theCollectionView.frame.size 
    })   
} 

override func viewWillLayoutSubviews() { 
    super.viewWillLayoutSubviews() 
    print("\(#function) \(view.frame.size)") 
} 

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 
    print("\(#function) \(view.frame.size)") 
} 

質問がいつ、どのように警告を回避し、スムーズなアニメーションを取得するためにUICollectionViewCellの変更を行う、ですが? viewWillTransition(:と:)(375.0、667.0) viewWillLayoutSubviews()(667.0、375.0) viewDidLayoutSubviews()(667.0、375.0)

デバイスの回転が起こる

、私は次のように見ることができます

viewDidLayoutSubviews()の直後にエラーが表示されます。 viewDidLayoutSubviews()でtheFlowLayout.invalidateLayout()を呼び出そうとしましたが、問題は変わりません。

私は最善の解決策を見つけていないが、少なくとも、それが動作し、私は他の人に同様の質問を読みましたが、私はあなたの助けを

おかげで、

セバスチャン

答えて

0

:-(私の答えを見つけることができませんでした。

私はviewWillTransitionを使用してセルのインデックスを計算し、回転後のオフセットをリセットします。アニメーションの貧弱さを避けるために、アルファを0に設定し、最初はアニメーションの最後に1を設定します。

viewWillLayoutSubviewsは、新しいセルサイズを設定するためにUICollectionViewFlowLayoutおよびviewDidLayoutSubviewsのレイアウトを無効にするために使用されます。

多分それは他人を助けるでしょうが、あなたがより良い解決策を持っていれば、それを共有してください!

@IBOutlet weak var theCollectionView: UICollectionView! { 
    didSet { 
     theCollectionView.backgroundColor = UIColor.black 
     theCollectionView.delegate = self 
     theCollectionView.dataSource = self 
    } 
} 

@IBOutlet weak var theFlowLayout: UICollectionViewFlowLayout! { 
    didSet { 
     theFlowLayout.itemSize = theCollectionView.frame.size 
     theFlowLayout.sectionInset = UIEdgeInsetsMake(0,0,0,0) 
     theFlowLayout.minimumLineSpacing = 0.0 
    } 
} 

var assets:[PHAsset]! 
var startAssetIndex = 0 

override func viewDidLoad() { 
    super.viewDidLoad() 
    view.backgroundColor = UIColor.black 
    automaticallyAdjustsScrollViewInsets = false 
    theCollectionView.reloadData() 
} 


override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    theCollectionView.scrollToItem(at: IndexPath(row:startAssetIndex, section:0), at: .left, animated: true) 
} 


/// Resize the collectionView during device rotation 
/// 
/// - Parameters: 
/// - size: New size of the viewController 
/// - coordinator: coordinator for the animation 
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
    super.viewWillTransition(to: size, with: coordinator) 

    // Compute the index of the image/video currently displayed 
    let offset = self.theCollectionView.contentOffset; 
    let index = round(offset.x/self.theCollectionView.bounds.size.width); 

    // hide the collection view to avoid horrible animation during the rotation 
    // animation is horrible due to the offset change 
    theCollectionView.alpha = 0.0 
    coordinator.animate(alongsideTransition: nil, completion: { 
     _ in 
     // display the collectionView during the animation 
     self.theCollectionView.alpha = 1.0 

     // compute the new offset based on the index and the new size 
     let newOffset = CGPoint(x: index * self.theCollectionView.frame.size.width, y: offset.y) 
     self.theCollectionView.setContentOffset(newOffset, animated: false) 
    }) 
} 


/// Invalidate the layout of the FlowLayout, it's mandatory for the rotation 
override func viewWillLayoutSubviews() { 
    theFlowLayout.invalidateLayout() 
    super.viewWillLayoutSubviews() 
} 


/// Set the size of the items (mandatory for the rotation) 
override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 
    theFlowLayout.itemSize = theCollectionView.frame.size 
} 
関連する問題