user1052610
user1052610

Reputation: 4719

UIScrollView won't stay in place after user finishes scrolling

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

Upvotes: 3

Views: 2738

Answers (3)

H Rivera
H Rivera

Reputation: 1

The app I was working on had similar symptoms. The user could scroll down but on release the view would snap back to the initial position. The page was set up as follow:

[VIEW] [SAFE AREA] [SCROLL VIEW] [CONTENT VIEW]

I strongly suspect that a combination of Auto-Layout and manual constraints caused by several adjustment iterations was causing the issue. To resolve this all constraints where removed from the View.

The Scroll View was assigned the following constraints:

Scroll View.leading = Safe Area.leading
Scroll View.top     = Safe Area.top
Scroll View.trailing= Safe Area.trailing
Scroll View.bottom  = Safe Area.bottom

The Content View was then assign the following constraints

ContentView.Leading = Scroll View.Leading
ContentView.top     = Scroll View.top
ContentView.centerX = ScrollView.centerX

The Content View was also given the following self constraint

height = 1000

Upvotes: 0

CalZone
CalZone

Reputation: 1721

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

CGSize scrollViewSize = CGSizeMake(newWidth, newHeight);
[self.myScrollView setContentSize:scrollViewSize];

Upvotes: 1

Damo
Damo

Reputation: 12900

Okay, the easiest way to get this scrollview working as you desire is to ensure that content size of the scrollview is identical to the frame size of the content you wish to scroll.

You can achieve this by having a content view into which you add all the views you wish to be visible and then add that content view to the scrollview while ensuring that the content size of the scrollview is set to the content view's frame size.

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

    UIView* contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1280, 460)];

    UIView* redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [redView setBackgroundColor:[UIColor redColor]];
    [contentView addSubview:redView];
    [redView release];

    UIView* blueView = [[UIView alloc] initWithFrame:CGRectMake(960, 0, 320, 460)];
    [redView setBackgroundColor:[UIColor blueColor]];
    [contentView addSubview:blueView];
    [blueView release];

    CGSize contentViewSize = contentView.frame.size;
    [scrollView addSubview:contentView];
    [scrollView setContentSize:contentViewSize];
    [contentView release];

    [self addSubview:scrollView];

Upvotes: 0

Related Questions