Frame size of UICollectionView is bigger than size of UIScreen

▼魔方 西西 提交于 2019-12-13 05:30:39

问题


In viewDidLoad func of my UIViewController I call

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

and it prints

320.0 375.0

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

Settings of this view in storyboard:

each constraint set as

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 width is 375 points and iPhone SE is 320:

Update

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

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/55667820/frame-size-of-uicollectionview-is-bigger-than-size-of-uiscreen

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