0
我有以下的CollectionView佈局(紅色)前後發生了改變佈局CollectiovView在導航控制器逼人退
如果我按到下一個控制器,然後按返回鍵,它看起來像這樣
如何避免這種情況發生呢?我的代碼如下
override func viewDidLoad() {
super.viewDidLoad()
screenSize = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
collectionView.backgroundColor = UIColor.whiteColor()
tableViewHeightConstant.constant = 5*44
collectionViewHeightConstant.constant = screenSize.width/2 * 2 // for testing purposes
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
layout.itemSize = CGSize(width: screenWidth/2, height: screenWidth/2)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 5
collectionView.setCollectionViewLayout(layout, animated: true)
}
你執行在'viewDidLoad'佈局的大小和位置的計算。視圖加載完成後不調用'viewDidLoad'。您是否曾嘗試在'viewWillAppear'中運行該代碼。 – Gordonium
放置它的更好的地方是'viewDidLayoutSubviews',每當superview' self.view'改變邊界時調用'viewDidLayoutSubviews'。 – mattsven
既不viewWillAppear,也不viewDidLayoutSubviews沒有幫助 –