简体   繁体   English

禁用ScrollView垂直滚动

[英]Disable ScrollView Vertical Scroll

In my app, I set the height of the UIScrollView programmatically to 70.0: 在我的应用程序中,我以编程方式将UIScrollView的高度设置为70.0:

在此输入图像描述

I have to dynamically set the width of the scrollView based on the number of images I add to it, which I do as follows: 我必须根据我添加到它的图像数量动态设置scrollView的宽度,我这样做如下:

int imageScrollerWidth = ([self.setSpins count]+1) * (int)([self.imageDimensions[@"width"] integerValue] + 5);
[self.spinScrollView setContentSize:CGSizeMake(imageScrollerWidth, self.spinScrollView.frame.size.height)];
self.spinScrollView.contentOffset = CGPointZero;

But when I open the view on my phone, it seems like I can scroll both vertically and horizontally: 但是当我在手机上打开视图时,似乎我可以垂直和水平滚动:

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

Since the scrollView size is set to 70, I didn't think vertical scrolling would be enabled. 由于scrollView大小设置为70,我不认为将启用垂直滚动。 How do I disable vertical scrolling? 如何禁用垂直滚动?

我修复了将此行添加到viewDidLoad的问题:

self.automaticallyAdjustsScrollViewInsets = NO;

Why you're still able to scroll vertically I can't answer rightaway, but I usually employ this trick: 为什么你仍然能够垂直滚动我无法马上回答,但我通常采用这个技巧:

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

Your own answer works as well - you can even set this option (Adjust Scroll View Insets) in the storyboard: 您自己的答案也可以 - 您甚至可以在故事板中设置此选项(调整滚动视图插入):

在此输入图像描述

Using a CollectionView could do the trick. 使用CollectionView可以做到这一点。 The implementation is pretty straight forward. 实施非常简单。 As a plus you get the - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath method to get the selected image. 作为一个加号你得到- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath方法来获取所选图像。

This are the required methods : 这是必需的方法:

- (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;
}

and on the storyboard you have to change the scroll orientation 在故事板上你必须改变滚动方向

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM