How to calculate iOS 11 size in different orientation?

可紊 提交于 2019-12-21 03:33:14

问题


I calculate itemSize dependent on safe area for UICollectionView with horizontal scroll and custom layout.

But for iPhone X safe area has different size for different orientation. My question is how should I calculate safe area size for landscape orientation in viewWillTransition function? Or how is it possible to do without this calculation?


回答1:


EDIT

To get safe area size without creating any additional views, use this:

view.safeAreaLayoutGuide.layoutFrame.size

If you want to use viewWillTransition method you can use this:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    // Before rotation
    print(view.safeAreaLayoutGuide.layoutFrame.size)

    coordinator.animate(alongsideTransition: { (context) in
        // During rotation
    }) { (context) in
        // After rotation
        print(self.view.safeAreaLayoutGuide.layoutFrame.size)
    }
}

In the completion block you will get your desired size, note however, that this code will be called after the rotation.

Original answer

Solution using additional UIView:

What I did was to create a UIView and pin it with constant 0 to Safe Area Guides, so that it always matches size of Safe Area:

I created an @IBOutlet of that UIView and in viewDidLayoutSubviews() check the size:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    print(containerView.frame.size)
}

After rotation I also get the updated size of that UIView.




回答2:


Use UICollectionViewDelegateFlowLayout's collectionView(_:layout:sizeForItemAt:). Works like a charm. Setting the size in the completion block of coordinator.animate(alongsideTransition:completion:) in viewWillTransition(to:with:) didn't work for me because the size animation occurs after the orientation animation ends, which looks weird.



来源:https://stackoverflow.com/questions/46299541/how-to-calculate-ios-11-size-in-different-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!