Wednesday, 6 April 2011

Tutorial for UIScrollView Programmatically (Vertical Scroll)

      With the help of  the UIScrollView class we can able to  display content that is larger than the size of the application’s window. It enables users to scroll the view and to see the full contents.

-Now we are going to create the UIScrollView Programmatically without using Interface Builder.

-creating the scroll view
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
-now we are going to create the views which are going to display in the scrollview.
-We are going to create 4 views.
-we are going to scroll that 4 views vertically.
NSInteger viewcount= 4; 
for (int i = 0; i <viewcount; i++) 
CGFloat y = i * self.view.frame.size.height; 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,                                                      self.view.frame.size.width, self .view.frame.size.height)];      
view.backgroundColor = [UIColor greenColor]; 
[scrollview addSubview:view]; 
[view release]; 
-y will place every UIView exactly where the previous UIView has stopped.
-The x and y coordinates for creating the new 4 views are.
-We are increasing the y coordinate only, so we can able to add the views vertically one after other.
-Set the UIScrollView contentSize
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount);
- ContentSize is nothing, it is size of the total view(Total size of 4 views)
-Here the content size is(320,1840)
You may also like
UINavigationController Programmatically
Adding an event to an UIImage

No comments:

Post a Comment