ios UICollectionView 布局问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16013209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:11:05  来源:igfitidea点击:

UICollectionView Layout Issue

iosuicollectionviewuicollectionviewlayout

提问by Nitesh

I am using UICollectionViewusing the flow layout. I have made a custom UICollectionViewCellfor the same. But on running the project the console keeps on throwing this error-

我正在使用UICollectionView流布局。我已经为此做了一个定制UICollectionViewCell。但是在运行项目时,控制台不断抛出这个错误-

The behavior of the UICollectionViewFlowLayout is not defined because:
 the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.

I have made sure that the size of the cell is correct

我已经确定单元格的大小是正确的

Has anyone been able to resolve this issue.

有没有人能够解决这个问题。

回答by Connor Wagner

I found that, using storyboards, you have to go into the storyboardand click on the overall View Controller(the view should be highlighted in blue) and go to the Attributes Inspector(the fourth tab on the right side of the screen) and unchecking the "Under Top Bars", "Under Bottom Bars", and "Under Opaque Bars." This cleared up the issue for me, and it cleared it for my coworkers as well.

我发现,使用storyboards,您必须进入storyboard并单击整体View Controller(视图应以蓝色突出显示)并转到Attributes Inspector(屏幕右侧的第四个选项卡)并取消选中“Under Top Bars” 、“底部条下”和“不透明条下”。这为我解决了问题,也为我的同事解决了问题。

回答by ninjaneer

I've been having some issues when the using a collection view to present full screen view controllers.

在使用集合视图呈现全屏视图控制器时,我遇到了一些问题。

Basically at run time, it'll ask for the size of the item and simply return the size of the collection view:

基本上在运行时,它会询问项目的大小并简单地返回集合视图的大小:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.collectionView.frame.size;
}

回答by Sajjon

I know this is a very late reply, but I have also experienced this.

我知道这是一个很晚的回复,但我也经历过这种情况。

Inside my view controller, let's call it MyViewControllerI have a UICollectionViewthat is using custom UICollectionViewCells. I'm implementing the method collectionView:layout:sizeForItemAtIndexPathwhere I am returning a item size that is dependent on the height of the UICollectionView.

在我的视图控制器中,我们称之为MyViewController我有一个UICollectionView使用自定义UICollectionViewCells 的。我正在实现方法collectionView:layout:sizeForItemAtIndexPath,我返回一个依赖于UICollectionView.

MyViewControlleris made in a storyboard using autolayout to control the height of the UIViewController.

MyViewController是在故事板中使用自动布局来控制UIViewController.

The cells look fine when in portrait mode, but did not when in landscape mode.

单元格在纵向模式下看起来很好,但在横向模式下则不然。

I solved my issues by invalidating the UICollectionViewLayoutof my UICollectionViewinside the viewWillLayoutSubviewsmethod.

我通过无效的解决我的问题,UICollectionViewLayout我的UICollectionView里面的viewWillLayoutSubviews方法。

- (void)viewWillLayoutSubviews {
    [self.myCollectionView.collectionViewLayout invalidateLayout];
}

Earlier I had tried to invalidate the layout inside willAnimateRotationToInterfaceOrientation:duration, but it didn't work. The reason for this is probably that the sizes for all the views are not calculated yet, so we have to wait until autolayout has finished its magic. I refer to this SO thread.

早些时候我曾试图使 里面的布局无效willAnimateRotationToInterfaceOrientation:duration,但没有奏效。原因可能是所有视图的大小都没有计算出来,所以我们必须等到自动布局完成它的魔力。我指的是这个 SO 线程

Update for Swift 4.0:

Swift 4.0 更新:

  override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.myCollectionView.collectionViewLayout.invalidateLayout()
  }

回答by Jonathan Crooke

Had this issue myself a few times when trying to create collection views with fullscreen cells. My problem was caused by laying out for 4" screen in IB/Storyboard and specifying 320*568 for item size, but running in the simulator using 3.5" screen, which has a height of 480. The solution is to specify your item size in code with something like:

在尝试使用全屏单元格创建集合视图时,我自己也遇到过几次这个问题。我的问题是由于在 IB/Storyboard 中布局 4" 屏幕并为项目大小指定 320*568,但在使用 3.5" 屏幕的模拟器中运行,其高度为 480。解决方案是在代码如下:

UICollectionViewFlowLayout *layout = (id) self.collectionView.collectionViewLayout;
layout.itemSize = self.collectionView.frame.size;

This ensures that the cell size is set correctly at runtime.

这可确保在运行时正确设置像元大小。

回答by DarkoM

If you are using storyboards and auto layout, debugging this kind of problems is really hard...

如果你正在使用故事板和自动布局,调试这类问题真的很难......

I had similar problem when trying to display UICollectionViewCell fullscreen on iPhone.

我在尝试在 iPhone 上全屏显示 UICollectionViewCell 时遇到了类似的问题。

Most of the time issue is with the size of the cell set in

大多数情况下,问题在于设置的单元格大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)

or directly on flowLayout.itemSize.

或直接在 flowLayout.itemSize 上。

But... try:

但是...尝试:

  1. Select the ViewController:
  1. 选择视图控制器:

Selecting View Controller

选择视图控制器

  1. Then uncheck Extend Edges options:
  1. 然后取消选中扩展边选项:

Disable Extend Edges Options

禁用扩展边选项

And now try setting your auto layout constraints.

现在尝试设置您的自动布局约束。

Good luck.

祝你好运。

回答by Nik Kov

That fixed my problem:

这解决了我的问题:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height - 70);
}

U can see padding value from top and bottom on this screen:

您可以在此屏幕上从顶部和底部看到填充值:

enter image description here

在此处输入图片说明

回答by spogebob92

What caused this for me was that I was trying to set my cells to the size of the superview rather than the size of the UICollectionView. Simply replaced the superview frame with the UICollectionViewframe.

对我造成这种情况的原因是我试图将我的单元格设置为超级视图的大小而不是UICollectionView. 简单地用框架替换超级视图UICollectionView框架。

回答by Dave Levy

For me I am using AKPickerView inside of a custom UIView which was giving me a slew of warnings like the ones mentioned here. All I did to eliminate the warnings was to Un-Check the box called 'AutoResize Subviews' in the Attributes Inspector for my custom UIView. That silenced the warnings so hard, I thought my logger stopped working.

对我来说,我在自定义 UIView 中使用 AKPickerView,它给了我很多警告,就像这里提到的那样。为了消除警告,我所做的只是在我的自定义 UIView 的属性检查器中取消选中名为“AutoResize Subviews”的框。这使警告变得如此困难,我以为我的记录器停止工作了。

enter image description here

在此处输入图片说明

回答by Oleg Prymak

Be sure to set the right autoresizing mask for UICollectionView and UICollectionviewcell. This fixed my problem for iPhone 4 Simulator

确保为 UICollectionView 和 UICollectionviewcell 设置正确的自动调整大小掩码。这解决了我的 iPhone 4 Simulator 问题

回答by SeanT

I just ran into the same error message, but for me the issue was that when I received new data from the api and tried to refresh my collectionView, the method that called [collectionView reloadData]wasn't calling it on the main thread.

我刚刚遇到了相同的错误消息,但对我来说,问题是当我从 api 接收新数据并尝试刷新我的 collectionView 时,[collectionView reloadData]调用的方法没有在主线程上调用它。

Hope this helps someone...

希望这可以帮助某人...