我有一个显示单元格垂直列表的 UICollectionView。它基本上是一个 TableView ,但我使用 Collection View 来更好地控制每个单元格在 View 中的放置方式。由于某种原因, Collection View 中的最后一个单元格被剪掉了。我可以向上拖动以查看下半部分,但一旦我放手, Collection View 就会弹回原位,遮住最后一个单元格的下半部分。我有一个包含 Collection View 的 xib,然后我从另一个 xib 注册 Collection View 单元格。整个 View 以编程方式放置在导航 Controller 中。我的想法是它必须是自动布局的问题。由于我以编程方式将包含 Collection View 的 View Controller 添加到导航 Controller ,因此在 Collection View 和导航栏之间没有设置任何约束。如果我隐藏实例化的导航栏并只是在 IB 中拖出一个,则 Collection View 单元格不会被剪裁。但我不想这样做。我试过以编程方式添加约束,但这也不起作用。有没有人有什么建议? 以下是我如何创建导航 Controller 并将包含 Collection View 的 View Controller 放入其中。此外,我尝试用于添加约束的代码也包括在内。目前,我在 didFinishLaunchingWithOptions 的应用程序委托(delegate)中执行此操作:
self.homeViewController = [[FFHomeViewController alloc] init];
FFNavigationPanelViewController *navigationController = [[FFNavigationPanelViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController];
// frontNavigationController.navigationBarHidden = YES;
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:navigationController];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.homeViewController.collectionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.homeViewController.navigationController.navigationBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
[self.viewController.frontViewController.view addConstraint:topConstraint];
最佳答案
遇到类似的问题,我有水平 collectionView
一行,我在很多地方使用过,不知何故不可点击。
我可以通过设置 collectionView
的 sectionInset
轻松解决这个问题。
如果您的 collectionView 在 ViewController 中,只需将这段代码放在 viewDidLoad()
中;如果它在 xib 中,则将此代码放在 init
方法中。
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.sectionInset.right = flowLayout.itemSize.width / 2
}
在我的例子中,collectionView.contentSize
中缺少的空白空间几乎等于单元格宽度的一半,但您可以将其设置为任何您想要的值。
我使用 .right
属性是因为我水平滚动单元格,在垂直滚动的情况下 collectionView
只需使用 .bottom
属性。
不是很完美的修复,但至少它有效。
关于ios - UICollectionView 中的最后一个单元格被剪裁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18409754/