UIScrollView will not retain position
Within my UIView, I have a UIScrollView which fills the containing view, so than when the content is bigger than the iPhone screen size, the user can scroll the page down. It works well, but when the user finishes the scroll movement - i.e. removes his fingers, the page snaps back into it's original position. Obviously that is not what I want, how should it be done?
Here is the relevant code in the UIView class which declares and uses the UIScrollView class. It draws a big red square in the middle of the screen, and allows the user to scroll:
@implementation TestView
- (id)initWithFrame: (CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.canCancelContentTouches=NO;
[self addSubview:scrollView];
CGSize scrollViewContentSize = CGSizeMake(320, 500);
[scrollView setContentSize:scrollViewContentSize];
CGRect rectForBigRedSquare = CGRectMake(50, 50, 200, 200);
UILabel *redSquare = [[UILabel alloc] initWithFrame:rectForBigRedSquare];
[redSquare setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:redSquare];
return self;
}
An additional question is this: how is it possible to make it such that the user can only scroll down, that is to see content at the bottom which was out of view, but not to scroll up so that there is space before the start of the content. Let's say the UIScrollView is filled with text which is bigger than the size of the view. Then you want to user to be able to scroll down to see all the text, but not to scroll up so that there is space between the start of the text and to top of the screen. Thanks very much!
|