This is the code that I use when setting up a UIScrollView to contain multiple images that can be scrolled through. It has PageControl which shows no. of image as well.
Call this method from your Desired method (viewDidLoad or viewWillAppear)
Call this method from your Desired method (viewDidLoad or viewWillAppear)
-(void) setupScrollView
{
//add the scrollview to the view
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
self.scrollView.pagingEnabled = YES;
[self.scrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
// add PageControl
self.pageControl = [[UIPageControl alloc] init];
self.pageControl.frame = CGRectMake(xOrigin+120, 175, 90, 37);
self.pageControl.numberOfPages = 5;
self.pageControl.pageIndicatorTintColor = [UIColor blackColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
self.pageControl.currentPage = i;
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[imgArray objectAtIndex:i]]]; // imgArray is Array of Images
image.contentMode = UIViewContentModeScaleToFill;
[self.scrollView addSubview:image];
}
//set the scroll view content size
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfViews,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:self.scrollView];
}
Thnx & Regards
Mohit Popat
No comments:
Post a Comment
Note: only a member of this blog may post a comment.