Horizontal UIPickerView

A situation can arise when you want to have some horizontal UIPickerView in your application.

Something like what in the picture shown below

horizontal_pickerview

Following code will draw one like that..

-(void)loadView:{
// Write code to create self.view
// Then...
pickerViewArray = [[NSArray arrayWithObjects:
 @"John Appleseed", @"Chris Armstrong", @"Serena Auroux",
 @"Susan Bean", @"Luis Becerra", @"Kate Bell", @"Alain Briere",
 nil] retain];
 myPickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
 myPickerView.delegate = self;
 myPickerView.showsSelectionIndicator =YES;
 myPickerView.backgroundColor = [UIColor clearColor];
 CGAffineTransform rotate = CGAffineTransformMakeRotation(-3.14/2);
 rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
 [self.myPickerView setTransform:rotate];
 [self.view addSubview:myPickerView];
} 

// UIPickerViewDelegate

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
	CGRect rect = CGRectMake(0, 0, 120, 80);
	UILabel *label = [[UILabel alloc]initWithFrame:rect];
	CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
	rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
	[label setTransform:rotate];
	label.text = [pickerViewArray objectAtIndex:row];
	label.font = [UIFont systemFontOfSize:22.0];
	label.textAlignment = UITextAlignmentCenter;
	label.numberOfLines = 2;
	label.lineBreakMode = UILineBreakModeWordWrap;
	label.backgroundColor = [UIColor clearColor];
	label.clipsToBounds = YES;
	return label ;
}

Courtesy: Syed Yusuf

Advertisements