UICollectionView的帧大小大于UIScreen的大小 [英] Frame size of UICollectionView is bigger than size of UIScreen

查看:72
本文介绍了UICollectionView的帧大小大于UIScreen的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我呼叫的UIViewControllerviewDidLoad函数中

print("\(UIScreen.main.bounds.size.width) \(collectionView.frame.width)")

它会打印

320.0 375.0

320.0 375.0

怎么可能? UICollectionView应该具有相同的宽度,mb较小,但绝对不能更大

How is it possible? UICollectionView should has the same width, mb smaller but definitely not bigger

情节提要中此视图的设置:

Settings of this view in storyboard:

每个约束设置为

所以我在iPhone SE Simulator上进行了测试,但在Storyboard中我有iPhone 8.

So I test it on iPhone SE Simulator but in Storyboard I have iPhone 8.

但是我相信当我使用自动布局时(应该使视图的大小与安全区域的大小相匹配)完全无关紧要

But I believe it should not matter at all when I use autolayout (constraints to make size of my view match size of safe area)

iPhone 8的宽度为375点,iPhone SE为320:

iPhone 8 width is 375 points and iPhone SE is 320:

更新

viewDidLoad似乎在情节提要中返回了您最后选择的设备的大小(非常丑陋)

It seems viewDidLoad returns sizes of your last selected device in storyboard (quite ugly)

因此,要获取有关视图的正确数据,我必须在viewDidAppear

So to get correct data about views I have to get sizes in viewDidAppear

在Android中,如果某些视图的大小尚不可用(视图尚未膨胀),则它仅返回零(宽度== 0,高度== 0),但是在iOS中,您可以从Xcode获取宽度和高度运行时的情节提要...真的吗?

In Android if size of some view isn't available yet (view wasn't inflated yet) then it just returns zeros (width == 0, height == 0), but in iOS you get width and height from Xcode Storyboard in runtime... Really?

推荐答案

从情节提要中实例化视图控制器时,其视图的大小将是当前在情节提要上设置的.将其添加到视图层次结构后,它将具有与屏幕相同的大小.在添加视图之前,将调用viewDidLoad,因此它的大小仍将与情节提要上的大小相同.您可以获得在内部方法viewWillApearviewDidAppearviewDidLayout中显示的大小.请记住,这些方法可能会多次调用.

When you're instantiating a view controller from storyboard, the size of its view will be what is currently set on the storyboard. It will have the same size as the screen after it has been added to the view hierarchy. viewDidLoad is called before the view is added so it will still have the same size as on the storyboard. You can get the size it is displayed in inside methods, viewWillApear, viewDidAppear, and viewDidLayout. Just keep in mind that these methods might be called multiple times.

这篇关于UICollectionView的帧大小大于UIScreen的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆