scientiffic
scientiffic

Reputation: 9415

Disable ScrollView Vertical Scroll

In my app, I set the height of the UIScrollView programmatically to 70.0:

enter image description here

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:

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

Since the scrollView size is set to 70, I didn't think vertical scrolling would be enabled. How do I disable vertical scrolling?

Upvotes: 3

Views: 1047

Answers (3)

Lucho
Lucho

Reputation: 1044

Using a CollectionView could do the trick. 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.

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

enter image description here

Upvotes: 0

Glorfindel
Glorfindel

Reputation: 22631

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:

enter image description here

Upvotes: 0

scientiffic
scientiffic

Reputation: 9415

I fixed the problem adding this line to viewDidLoad:

self.automaticallyAdjustsScrollViewInsets = NO;

Upvotes: 3

Related Questions