How to enable scrolling in scroll view in Objective-C?

Total Post:23

Points:161
 1161  View(s)
Ratings:
Rate this:

I want to enable scrolling in our scroll view. I have created an app using scrollview and set an image. using this code:

UIImageView *imgView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wall-1.jpg"]];
scrollview.scrollEnabled=YES;
[scrollview addSubview:imgView];

In the above code I also set scrollEnabled property to YES using scrollview outlet, but scrolling is not working.
Please suggest me how can I enable the scrolling feature in our application.
Thanks.

  1. Post:214

    Points:1642
    Re: How to enable scrolling in scroll view in Objective-C?

    Your code is correct but needed one thing more to do. To enable scrolling in your scrollview, set the content size of scrollview and your problem will be solved.
    Below I am providing complete code, in that code I have added one extra line of code:

    UIImage *img = [UIImage imageNamed:@"wall-1.jpg"];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    scrollview.scrollEnabled = YES;
    // this code is responsible for scrolling in your scrollview.
    [scrollview setContentSize:CGSizeMake(img.size.width, img.size.height)];
    [scrollview addSubview:imgView];

    That's it. after using above code you will see scrolling functionality is enabled now.
    Now I want to tell you that you called a method scrollEnabled, there is no need to set this here, without using this scrolling will work perfectly.

Answer