Home > DeveloperSection > Forums > How to enable scrolling in scroll view in Objective-C?
zack mathews
zack mathews

Total Post:23

Posted on    February-15-2016 11:03 PM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 815  View(s)
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 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.

Tarun Kumar

Total Post:214

Posted on    February-15-2016 11:30 PM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick