Adam Altinkaya
Adam Altinkaya

Reputation: 639

UIScrollview doesn't scroll iOS

I'm creating several scroll views in one view that scroll horizontally. The following code works fine:

-(void)updateSection {
    builder = [NSMutableArray array];
    float xPosition = 0;
    float xposbut = 100;

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.frame.size.width, self.frame.size.height - 69)];

    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.delegate = self;
    [self addSubview:scrollView];

    for (int i = 1; i < itemArticleArray.count; i++) {
        ItemView *item = [ItemView loadNibs];
        item.frame = CGRectMake(xposbut, 10, item.frame.size.width, item.frame.size.height);

        xPosition += scrollView.frame.size.width + 2;
        xposbut += 500;
        UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xposbut - 50, 4, 2, scrollView.frame.size.height - 8)];
        [scrollView addSubview:seperatorView];
        xPosition += scrollView.frame.size.width + 4;

        [scrollView addSubview: item];
        [builder addObject:item];
    }

    [scrollView setContentSize:CGSizeMake(xposbut, scrollView.frame.size.height)];
    [self addSubview:scrollView];
}

However, when I change the 8th line of code to the following:

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 69)];

It cause the layout to look fine, but the scroll views do then not scroll at all. Any ideas?

Upvotes: 0

Views: 2772

Answers (4)

davidrl1000
davidrl1000

Reputation: 361

You must give the scroll some space to work so check this out:

  1. Create the scroll in the storyboard
  2. Create an outlet for the scroll
  3. And the in the .m file you must paste these lines

The code:

(void)viewDidLoad {
    [super viewDidLoad];
    yourScroll.scrollEnabled=YES;
    yourScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2);
}

/*
 * yourScroll.contentSize=CGSizeMake(your desired scroll width, your desired scroll height);
 * The values must be bigger than the viewController size
 */

Upvotes: 0

MetalJr
MetalJr

Reputation: 302

Set the content size for the scroll view. The content size should be greater than frame.size, then only the scroll view will scroll. And also enable scrolling.

Use the code

scrollView.scrollEnabled=YES;
scrollView.contentSize=CGSizeMake(CGFloat width, CGFloat height);

Height should be greater than scrollView.frame.size.height and add contents inside scrollview beyond the scrolview's frame.

Upvotes: 0

Abdullah Md. Zubair
Abdullah Md. Zubair

Reputation: 3324

UIScrollview will scroll when content size is bigger then frame size. In your code you set content size equal to frame size that's why your scrollview is not scrolling.

Upvotes: 0

thorb65
thorb65

Reputation: 2716

you set the contentSize.height to the scrollView.frame.size.height and so the scrollView cant scroll. you have to set the contentSize to the total height of you scrollView, including the not visible area. The frame is only the area on screen.

Upvotes: 3

Related Questions