1

我正在使用UICollectionView創建全屏圖像庫。當用戶旋轉設備,我執行更新到UICollectionViewSwift viewWillTransition not called

func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)

我模態呈現這個UIViewController,有一個UICollectionView佔據整個屏幕。在viewDidLoad,我創建了流佈局:

let flowLayout = UICollectionViewFlowLayout() 
flowLayout.scrollDirection = .horizontal 
flowLayout.minimumInteritemSpacing = 0 
flowLayout.minimumLineSpacing = 0 
photosCollectionView.isPagingEnabled = true 
photosCollectionView.setCollectionViewLayout(flowLayout, animated: true) 

我也有作爲大小:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return photosCollectionView.frame.size 
} 

當我轉動我的設備,viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)不會被調用,這會導致UICollectionViewLayout不更新。雖然我旋轉設備,我得到的消息:

The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. 

我讀過網上,我可以添加:

self.automaticallyAdjustsScrollViewInsets = false 

UIViewController,但也沒有影響。沒有內容或部分插入UICollectionView

我也在函數中調用了super.viewWillTransition。任何人都可以幫助我什麼可能導致這個問題?

+0

錯誤消息提到「物品高度」。我沒有看到你的代碼確定。你能證明嗎? – matt

+0

當我設置大小時,就是高度和寬度。我在'IndexPath' ='photosCollectionView.frame.size'設置'sizeForItem'。 –

+0

好的,但如果你在錯誤的時間說,我可以想象,你可能會得到一個尺寸,其中一個或兩個尺寸比集合視圖最終變得更大。 – matt

回答

2

如果你只是佈局的關注時,該設備,然後旋轉,請使用:

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    collectionView.collectionViewLayout.invalidateLayout() 
    collectionView.layoutIfNeeded() 
} 

從蘋果文檔:

public func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) 

該方法被調用時,視圖控制器的視圖的大小是 更改其父(即對於根視圖控制器時,其 窗口旋轉或調整大小)。 如果您重寫此方法,則應調用super將更改傳播給子節點,或手動將更改傳遞給 子節點。

我猜你可能會要求該視圖的父這一功能,而無需調用各地超級

一個工作也將是該設備旋轉登記:

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

    NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 
} 

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 

    NotificationCenter.default.removeObserver(self) 
} 

func deviceOrientationDidChange(_ notification: Notification) { 
    let orientation = UIDevice.current.orientation 
    print(orientation) 
} 
+0

這是一種可能性,但它不能解釋爲什麼'viewWillTransition'函數沒有被調用。讓我懷疑我是否有錯誤的設置。 –

+0

,因爲它在文檔中說它應該被調用,除非你調用它父母沒有調用超級,所以而不是去你的代碼我會建議以上的方式 – zombie

+0

我已經雙重檢查了所有'超'函數被調用。我也用iPad測試過,出於某種原因,在iPad上調用'viewWillTransition'並沒有問題,只是沒有iPhone。 –

0

這是怎麼了我固定爲:

轉到項目目標 - 常規 - 部署信息

打勾設備方向ations的應用程序支持

而且你需要(無論什麼是適當的更改UIInterfaceOrientationMask.all):

override var supportedInterfaceOrientations : UIInterfaceOrientationMask { 
    get { 
     return UIInterfaceOrientationMask.all; 
    } 
} 

的視圖控制器。