以编程方式创建的UICollectionView不会滚动

Ale*_*son 3 ios uicollectionview

我正在使用一个水平选择器UICollectionView.它很简单:A UIViewUICollectionView编程方式创建,使用UICollectionViewFlowLayout一个部分,滚动设置为水平.它出现在屏幕上,在正确的单元格中显示正确的数据.但它不会滚动 - 实际上它根本不响应用户交互.

这是视图的初始化程序:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

我尝试以编程方式设置UserInteractionEnabledYES,但这没有任何区别(也不应该有,因为默认UserInteractionEnabled设置为YES).FWIW,集合视图使用标准UICollectionViewCells,s作为子视图UILabel添加到contentViews中.

有没有想过为什么这不滚动?任何和所有帮助非常感谢.

Ale*_*son 6

好吧,这对我来说既愚蠢又容易修复.我将集合视图的框架设置为其父视图frame而不是它的视图bounds.这导致了各种自动布局问题,导致触摸事件无法注册.全部修复了.