What is the “right” way to handle orientation changes in iOS 8?

前端 未结 4 670
感情败类
感情败类 2020-11-29 16:06

Can someone please tell me the \"right\" or \"best\" approach to working with portrait and landscape interface orientations in iOS 8? It seems that all the functions I want

4条回答
  •  清酒与你
    2020-11-29 16:08

    Based on smileyborg's very well detailed (and accepted) answer, here is an adaptation using swift 3:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: nil, completion: {
            _ in
            self.collectionView.collectionViewLayout.invalidateLayout()
        })        
    }
    

    And in the UICollectionViewDelegateFlowLayout implementation,

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // retrieve the updated bounds
        let itemWidth = collectionView.bounds.width
        let itemHeight = collectionView.bounds.height
        // do whatever you need to do to adapt to the new size
    }
    

提交回复
热议问题