2
votes

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.

without nav controllerwith nav controller

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?

2
Where is this code being called from? My guess is you are doing this in -viewDidLoad.Jeffery Thomas
@JefferyThomas you're right, it's being called from -viewDidLoad. Don't you mean that in -viewDidLoad frames are not set yet and I should move it to -viewWillAppear?mrvn

2 Answers

1
votes

You can create and add views in -viewDidLoad, but you will need to use auto sizing or manually set the frames in -viewWillAppear: or some other method that fires after self.view has been sized.

Bounds and Frame size in viewDidLoad

0
votes

First of all, what is this??

scrollViewSize

You have used it at 2-3 places, like here:

CGFloat imageDisplayedWidth = scrollViewSize.width - (2 * imageOffsetX);

if it is the size of your scrollView's frame then, you better use simply

self.scrollView.frame.size

otherwise.. I can't get you..

Secondly, when your UINavigationBar object comes in the view it "eats" some space of your screen. By default its height is 44 pixels (in non retina).

Due to this your other views and subviews also shift down. Even your main view gets shifted down by 44 points, so, this is the reason why your whole view alongwith your scrollView and your images on it shift down. But, your view's height still remains 460!(in iPhone 4, 4S) and that extra 44 points go out of the bounds of your screen..i.e. out of the bottom of the screen(so! it should come out from the bottom of your phone ;) !!) so you will have to look out for it also...

What I will suggest you to do is set the frame of scrollView to a reduced height: Add these lines to your code.. better add it at the start of your method :

`self.scrollview.frame=CGRectMake(0,0,320,self.view.frame.size.height-44)

// if it is the default navigation bar.. otherwise you can subtract that height which you created in your custom bar

All other things should work fine, according to your code.. Do give a feedback.. if you use it :]