Reputation: 3767
So I have seen the many other posts on this, and I think I have tried almost all of them. I don't want this to be a duplicate, but I can't get a solution to my problem. I have a setup like this:
When I get to my Scroll View Controller, I can page over just fine, but I can also move the pictures around vertically. I think it has something to do with the NavigationBar forcing the ScrollView frame down, but still having the frame set to the full screen size. How do I prevent any vertical scrolling on the Scroll View View Controller? My .m is below:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBarHidden = NO;
i = 0;
_PhotoBundle = [[NSBundle mainBundle] pathsForResourcesOfType:@".jpg"inDirectory:@"Dog_Images"];
_PhotoArray = [[NSMutableArray alloc] initWithCapacity:_PhotoBundle.count];
for (NSString* path in _PhotoBundle)
{
[_PhotoArray addObject:[UIImage imageWithContentsOfFile:path]];
}
for (int x = 0; x < _PhotoArray.count; x++)
{
CGRect frame;
frame.origin.x = self.mainScroll.frame.size.width * x;
frame.origin.y = 0;
frame.size = self.mainScroll.frame.size;
UIImage *nextImg = [[UIImage alloc] init];
nextImg = [_PhotoArray objectAtIndex:x];
UIImageView *nextIV = [[UIImageView alloc] initWithFrame:frame];
[nextIV setImage:nextImg];
[self.mainScroll addSubview:nextIV];
//NSLog(@"Pass %d", x);
}
self.mainScroll.contentSize = CGSizeMake(self.mainScroll.frame.size.width * _PhotoArray.count, self.mainScroll.frame.size.height);
}
Thank you very much!!
Upvotes: 2
Views: 2167
Reputation: 3767
So I found a post that explained it perfectly:
How to disable just vertical scrolling in a UIScrollView?
When I change my
CGSizeMake(self.mainScroll.frame.size.width * _PhotoArray.count, self.mainScroll.frame.size.height);
to
CGSizeMake(self.mainScroll.frame.size.width * _PhotoArray.count, 1.0);
It makes the contentSize from being larger than the bounds... Something that I have read about, but did not fully understand. I hope this helps someone else who is stuck with this...
Upvotes: 6
Reputation: 1645
UIScrollView
scrolls it's content only if it's -contentSize
is set bigger than it's frame. So when you are setting self.mainScroll.contentSize
it has contentSize.height
more that it's frame.
Try reading at least Apple's documentation before using it's classes. It's boring. But salary pays off all the boring stuff.
By the way. You are using UIScrollView incorrectly for your displaying UIImageView objects. Try to watch Apple WWDC sessions (which could be downloaded from iTunes) - there are two or three sessions about how to use just three UIImageView objects to draw an endless UIScrollView paging
Apple's documentation on UIScrollView
Upvotes: 1