Disable ScrollView scrolling

In my application, I was setting the height of the UIScrollView programmatically to 70.0:

enter image description here

I need to dynamically set the width of the scrollView depending on the number of images I added, which I do like this:

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 that I can scroll both vertically and horizontally:

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

Since the scrollView size is 70 I didn't think vertical scrolling would be enabled. How do I turn off vertical scrolling?

+3


source to share


3 answers


I fixed the problem with adding this line to viewDidLoad:



self.automaticallyAdjustsScrollViewInsets = NO;

      

+3


source


Why can you still scroll vertically, I can't answer right away, but I usually use this trick:

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

      



Your own answer also works - you can even set this option (Customize Scrolling) in the storyboard:

enter image description here

0


source


Using a CollectionView might do the trick. The implementation is pretty straight forward. As a plus, you get a method - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

to get the selected image.

These 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

enter image description here

0


source







All Articles