禁用ScrollView垂直滚动

时间:2015-08-06 19:22:49

标签: ios objective-c uiscrollview

在我的应用程序中,我以编程方式将UIScrollView的高度设置为70.0:

enter image description here

我必须根据我添加的图像数量动态设置scrollView的宽度,我的操作如下:

{{1}}

但是当我在手机上打开视图时,似乎我可以垂直和水平滚动:

https://youtu.be/5QplkgQ-viU

由于scrollView大小设置为70,我不认为将启用垂直滚动。如何禁用垂直滚动?

3 个答案:

答案 0 :(得分:3)

我修正了将此行添加到viewDidLoad:

的问题
self.automaticallyAdjustsScrollViewInsets = NO;

答案 1 :(得分:0)

为什么你仍然能够垂直滚动我无法立即回答,但我通常会采用这个技巧:

[self.spinScrollView setContentSize:CGSizeMake(imageScrollerWidth, 0)];

您自己的答案也可以 - 您甚至可以在故事板中设置此选项(调整滚动视图插入):

enter image description here

答案 2 :(得分:0)

使用CollectionView可以解决问题。实施非常简单。 作为加分,您可以使用- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath方法获取所选图像。

这是必需的方法:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 30;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellectionCell" forIndexPath:indexPath];

    [cell addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]]];
    return cell;
}

在故事板上你必须改变滚动方向

enter image description here

相关问题