以编程方式创建的UICollectionView不会滚动 [英] UICollectionView created programmatically doesn't scroll
问题描述
我正在使用 UICollectionView
制作水平选择器。这很简单:使用 UICollectionViewFlowLayout $ c,以编程方式创建
UIView
,并以 UICollectionView
创建$ c>有一个部分,滚动设置为水平。它出现在屏幕上,在正确的单元格中显示正确的数据。但它不会滚动 - 事实上它根本不会响应用户交互。
I'm making a horizontal picker by using a UICollectionView
. It's simple enough: A UIView
with a UICollectionView
created programmatically, using UICollectionViewFlowLayout
with one section, scrolling set to horizontal. It appears onscreen, complete with the correct data in the correct cells. But it doesn't scroll---in fact it doesn't respond to user interaction at all.
这是视图的初始化程序:
Here's the initializer for the view:
- (id)initWithFrame:(CGRect)frame andItemData:(NSArray *)itemData
{
self = [super initWithFrame:frame];
if (self) {
_itemData = itemData;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setItemSize:CGSizeMake(kCellWidth, kCellHeight)];
[flowLayout setMinimumInteritemSpacing:0.f];
[flowLayout setMinimumLineSpacing:0.f];
_collectionView = [[UICollectionView alloc] initWithFrame:[self frame] collectionViewLayout:flowLayout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView setBounces:NO];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"HorizontalPickerCell"];
[self addSubview:_collectionView];
}
return self;
}
我尝试以编程方式设置 UserInteractionEnabled
到 YES
,但这没有任何区别(也不应该有,因为 UserInteractionEnabled
设置为 YES
默认情况下)。 FWIW,集合视图使用标准的 UICollectionViewCell
,并将 UILabel
添加到他们的 contentView
s作为子视图。
I tried programmatically setting UserInteractionEnabled
to YES
, but that didn't make any difference (nor should it have, since UserInteractionEnabled
is set to YES
by default). FWIW, the collection view uses standard UICollectionViewCell
s with UILabel
s added to their contentView
s as subviews.
是否考虑过为什么不滚动?任何和所有的帮助非常感谢。
Any thought as to why this isn't scrolling? Any and all help much appreciated.
推荐答案
好吧,这对我来说都是愚蠢的,很容易修复。我将集合视图的框架设置为其父视图的框架
而不是其边界
。这导致了各种自动布局问题,导致触摸事件无法注册。现在全部修好了。
Ok, this turned out to be both dumb on my part and easy to fix. I set the frame of the collection view to its parent view's frame
rather than its bounds
. This caused all sorts of autolayout issues and resulted in touch events simply not registering. All fixed now.
这篇关于以编程方式创建的UICollectionView不会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!