Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView won't stay in place after user finishes scrolling

Tags:

ios

Within my UIView, I have a UIScrollView which fills the first 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 can it be avoided?

Here is the relevant code in the UIView class which declares and uses the UIScrollView class.

@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. In

like image 863
user1052610 Avatar asked Dec 21 '25 07:12

user1052610


1 Answers

Basically you just have to set contentSize of your scrollview according to the contents.

CGSize scrollViewSize = CGSizeMake(newWidth, newHeight);
[self.myScrollView setContentSize:scrollViewSize];
like image 138
CalZone Avatar answered Dec 22 '25 20:12

CalZone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!