I'm trying to get UIScrollView working in my training app. Scroll itself works fine, but if I'm using a Navigation Controller, the content area of the SVC is shifted downwards for amount of navigation bar height.
Cyan area is UIScrollView background. contentSize is set properly, only swiping pages side to side is allowed. This is how I add an image to scroll view
CGSize scrollViewSize = self.scrollView.frame.size;
UIImage * image = [UIImage imageWithContentsOfFile:[cachesDirectory stringByAppendingFormat:@"/%@", fileName]];
CGFloat imageOriginalWidth = image.size.width;
CGFloat imageOriginalHeight = image.size.height;
CGFloat imageOffsetX = 10;
CGFloat imageDisplayedWidth = scrollViewSize.width - (2 * imageOffsetX);
CGFloat imageDisplayedHeight = imageOriginalHeight / imageOriginalWidth * imageDisplayedWidth;
CGFloat imageOffsetY = (scrollViewSize.height - imageDisplayedHeight) / 2;
CGRect viewFrame = CGRectMake(scrollViewSize.width * (filmIndex) + imageOffsetX,
imageOffsetY,
imageDisplayedWidth,
imageDisplayedHeight);
contentAreaHeight = imageDisplayedHeight;
UIImageView * view = [[UIImageView alloc] initWithFrame:viewFrame];
view.backgroundColor = [UIColor greenColor];
view.image = image;
view.contentMode = UIViewContentModeScaleAspectFit;
view.tag = filmIndex;
[self.scrollView addSubview:view];
And this is how I set content size and offset
self.scrollView.contentSize = CGSizeMake(scrollViewSize.width * ([self.filmsData count]),
contentAreaHeight);
self.scrollView.contentOffset = CGPointMake(scrollViewSize.width * [self.filmsData count] / 2,
0);
How what's wrong with my code and how can I get images centered vertically when using navigation controller?
-viewDidLoad
. – Jeffery Thomas-viewDidLoad
. Don't you mean that in-viewDidLoad
frame
s are not set yet and I should move it to-viewWillAppear
? – mrvn