如何计算iOS 11大小的不同方向? [英] How to calculate iOS 11 size in different orientation?
问题描述
我使用水平滚动和自定义布局计算取决于UICollectionView安全区域的itemSize。
I calculate itemSize dependent on safe area for UICollectionView with horizontal scroll and custom layout.
但对于iPhone X,安全区域的大小不同,方向不同。我的问题是如何在viewWillTransition函数中计算横向的安全区域大小?或者如何在没有这个计算的情况下做到这一点?
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?
推荐答案
编辑
要获得安全区域大小而不创建任何其他视图,请使用:
To get safe area size without creating any additional views, use this:
view.safeAreaLayoutGuide.layoutFrame.size
如果你想使用 viewWillTransition
方法你可以使用它:
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.
原始答案
使用额外UIView的解决方案:
Solution using additional UIView:
我所做的是创建一个 UIView
并将其与常量0固定到安全区域指南,以便它始终与安全区域的大小相匹配:
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:
我创建了一个 @IBOutlet
的 UIView
并在 viewDidLayoutSubviews()
检查大小:
I created an @IBOutlet
of that UIView
and in viewDidLayoutSubviews()
check the size:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(containerView.frame.size)
}
轮换后我也得到了UIView的更新大小。
After rotation I also get the updated size of that UIView.
这篇关于如何计算iOS 11大小的不同方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!