Create UIScrollView in iPhone Objective C

Today, we will create a scroll view using the UIScrollView class and add the alphabets in the scroll view. The frame values used should be changed as per the needs. Action method for these buttons can also be added.


#define WIDTH_OF_SCROLL_PAGE 65
#define HEIGHT_OF_SCROLL_PAGE 40
#define WIDTH_OF_IMAGE 65



CGRect scrollframe = CGRectMake(0.0, 4.0, 320, 40);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollframe];
scrollView.bounces = YES;
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;





NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];


for (int i = 0; i < [array count]; i++) {


NSString *name = [array objectAtIndex:i];

UIButton *btn = [[UIButton alloc] initWithFrame:frame];
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[btn setTitle:name forState:UIControlStateNormal];

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
btn.enabled = TRUE;
btn.tag = i+1;

[scrollView addSubview:btn];
[btn release];

}


[scrollView setContentSize:CGSizeMake(WIDTH_OF_IMAGE * 500, HEIGHT_OF_IMAGE)];
[scrollView setContentOffset:CGPointMake(WIDTH_OF_IMAGE * 258, 0)];
scrollViewOffset = WIDTH_OF_IMAGE * 258;
scrollView.pagingEnabled = YES;
[self.view addSubview:scrollView];

Comments

Popular posts from this blog

Integrating ZXing QR Code reader in iPhone / iOS applications

Multiple line of text in UIPickerView

Connect Samsung devices to Kies on Mac