7

How can I use UIScrollView? please give me a simple example with one scrolling image?

Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
M Faheem Rajput
  • 768
  • 2
  • 9
  • 27

4 Answers4

4

This can be one example. Basically we create a scrollview, set its frame, add content as a subview, and then set the content size. The image (iphone.png) below is bigger than the iphone screen so that we can scroll it.

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iphone.png"]];

[scrollView addSubview:imageView];
[imageView release];
scrollView.contentSize = CGSizeMake(imageView.image.size.width, imageView.image.size.height);
[window addSubview:scrollView];
[scrollView release];
Khomsan
  • 543
  • 5
  • 5
  • what is this line done ? in my code it is generate the error for window word. [window addSubview:scrollView]; – M Faheem Rajput May 05 '11 at 05:57
  • I test by putting this code in didFinishLaunchingWithOptions above the [self.window makeKeyAndVisible], and it work fine. Actually window is the top window that is the parent of all view. I think you can put your parent view instead of window. – Khomsan May 05 '11 at 11:30
  • Sorry I forgot to answer. What this line done is adding a scrollview to its parent view. – Khomsan May 05 '11 at 11:40
1

You can use below code to add UIScrollView on yourView :-

[self.ScrollView setContentSize:(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height))];

Step:1 Create a UiScrollView,

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIScrollView *MyScrollView;
@end

Step2: In your ViewController.m,

[self.MyScrollView setContentSize:(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height))];
Jeff John
  • 31
  • 3
0

Add UIImageView in to UIScrollView.

then,

Use

imageView.image = [UIImage imageNamed:@"image1.png"];
imageView.frame = CGRectMake(0.0, 0.0, imageView.image.size.width, imageView.image.size.height);
scrollView.contentSize = CGSizeMake(imageView.image.size.width, imageView.image.size.height);
Chetan Bhalara
  • 10,326
  • 6
  • 32
  • 51