Wednesday, 6 April 2011

UIScrollView Programmatically(Horizontal 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 Horizontally.

    NSInteger viewcount= 4;
    for (int i = 0; i <viewcount; i++) 

    CGFloat y = i * self.view.frame.size.width;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, 0, self.view.frame.size.width,           self.view.frame.size.height)];
    view.backgroundColor = [UIColor greenColor];
    [scrollview addSubview:view];
    [view release];

-x 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 x coordinate only, so we can able to add the views horizontally one after other.

-Set the UIScrollView contentSize

scrollview.contentSize = CGSizeMake(self.view.frame.size.width * viewcount, self.view.frame.size.height);
-ContentSize is nothing, it is size of the total view(Total size of 4 views)
-Here the content size is(1280,480)

No comments:

Post a Comment