この質問に答えるには遅すぎますが、私は最近この問題を経験しました。
@leeの上記の答えは、私の答えを得るのに大いに役立ちます。しかし、その答えはObjective-cに限定されており、私は迅速に作業していました。
@leeあなたの答えを参考にして、迅速なユーザーがこの問題を簡単に解決できるようにします。そのためには、以下の手順に従ってください。
宣言CGFloat
セクションで変数を宣言します。
var height : CGFloat!
あなたはそれviewDidAppear
を得ることができます:
height = self.myCollectionView.collectionViewLayout.collectionViewContentSize().height
たぶん、reload
データが新しいデータで新しい高さを計算する必要があるときは、次の方法で取得addObserver
できCollectionView
ますviewWillAppear
。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
....
....
self.shapeCollectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.Old, context: nil)
}
collectionview
次に、以下の関数を追加して、新しい高さを取得するか、リロードが完了した後に何かを実行します。
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
let newHeight : CGFloat = self.myCollectionView.collectionViewLayout.collectionViewContentSize().height
var frame : CGRect! = self.myCollectionView.frame
frame.size.height = newHeight
self.myCollectionView.frame = frame
}
そして、オブザーバーを削除することを忘れないでください:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
....
....
self.myCollectionView.removeObserver(self, forKeyPath: "contentSize")
}
これが問題を迅速に解決するのに役立つことを願っています。