UICombo.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Prince Kumar Sharma on 27/02/13. | |
// Copyright (c) 2013 Prince Kumar Sharma All rights reserved. | |
#import <UIKit/UIKit.h> | |
#import <QuartzCore/QuartzCore.h> | |
typedef enum { | |
CBRed, | |
CBBlue, | |
CBGray, | |
CBLightGray, | |
CBYellow, | |
CBMagenta, | |
CBBrown, | |
CBPurple, | |
CBBlack | |
} CBColor; | |
@class UIViewController; | |
@interface UICombo : UIView<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIScrollViewDelegate> | |
{ | |
@private | |
int numberOfObjects; | |
UILabel *arrow; | |
NSArray *items; | |
UITableView *_tableView; | |
UITextField *_textField; | |
} | |
- (id)initWithFrame:(CGRect)frame andItems:(NSArray*)itemArray andColor:(CBColor)color; | |
-(void)addItem:(NSString*)item; | |
-(void)insertItem:(NSString*)item AtIndex:(int)index; | |
-(int)removeItem; | |
-(void)removeItemAtIndex:(NSUInteger)index; | |
-(void)refresh; | |
@end |
UICombo.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Prince Kumar Sharma on 27/02/13. | |
// Copyright (c) 2013 Prince Kumar Sharma All rights reserved. | |
#import "UICombo.h" | |
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI) | |
@implementation UICombo | |
- (id)initWithFrame:(CGRect)frame andItems:(NSArray*)itemArray andColor:(CBColor)color | |
{ | |
if ((self = [super initWithFrame:frame])) | |
{ | |
self->items=[[NSArray alloc]initWithArray:itemArray]; | |
self->numberOfObjects=[self->items count]; | |
[self setBackgroundColor:[UIColor lightGrayColor]]; | |
[self setAutoresizesSubviews:YES]; | |
_textField=[[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)]; | |
[_textField.layer setCornerRadius:5.0]; | |
[_textField setBorderStyle:UITextBorderStyleBezel]; | |
if (self->items.count!=0) { | |
[_textField setText:[self->items objectAtIndex:0]]; | |
} | |
[_textField setFont:[UIFont systemFontOfSize:15.0f]]; | |
[_textField setDelegate:self]; | |
[_textField addTarget:self action:@selector(open) forControlEvents:UIControlEventTouchDown]; | |
arrow=[[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-20,5,20,20)]; | |
[arrow setText:@">"]; | |
[arrow setTextAlignment:NSTextAlignmentCenter]; | |
[arrow setFont:[UIFont boldSystemFontOfSize:14.0f]]; | |
[arrow setBackgroundColor:[UIColor clearColor]]; | |
arrow.layer.anchorPoint=CGPointMake(0.5,0.5); | |
[arrow setTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90))]; | |
[_textField addSubview:arrow]; | |
switch (color) { | |
case CBRed: | |
[_textField setTextColor:[UIColor redColor]]; | |
break; | |
case CBBlue: | |
[_textField setTextColor:[UIColor blueColor]]; | |
break; | |
case CBGray: | |
[_textField setTextColor:[UIColor grayColor]]; | |
break; | |
case CBLightGray: | |
[_textField setTextColor:[UIColor lightGrayColor]]; | |
break; | |
case CBYellow: | |
[_textField setTextColor:[UIColor yellowColor]]; | |
break; | |
case CBMagenta: | |
[_textField setTextColor:[UIColor magentaColor]]; | |
break; | |
case CBBrown: | |
[_textField setTextColor:[UIColor brownColor]]; | |
break; | |
case CBPurple: | |
[_textField setTextColor:[UIColor purpleColor]]; | |
break; | |
case CBBlack: | |
default: | |
[_textField setTextColor:[UIColor blackColor]]; | |
break; | |
} | |
CAGradientLayer *gradient = [CAGradientLayer layer]; | |
gradient.frame = _textField.bounds; | |
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor lightGrayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; | |
[_textField.layer insertSublayer:gradient atIndex:0]; | |
_tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,_textField.frame.size.height, self.frame.size.width,0)]; | |
_tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight; | |
[_tableView setBackgroundColor:[UIColor clearColor]]; | |
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; | |
[_tableView setRowHeight:25]; | |
[_tableView setDelegate:self]; | |
[_tableView setDataSource:self]; | |
[_tableView.layer setMasksToBounds:YES]; | |
[_tableView.layer setCornerRadius:2.0]; | |
[_tableView.layer setBorderColor:[UIColor lightGrayColor].CGColor]; | |
[_tableView.layer setBorderWidth:1.5f]; | |
[_tableView.layer setShadowColor:[UIColor blackColor].CGColor]; | |
[_tableView.layer setShadowOpacity:0.8]; | |
[_tableView.layer setShadowRadius:3.0]; | |
[_tableView.layer setShadowOffset:CGSizeMake(2.0, 2.0)]; | |
[self addSubview:_textField]; | |
[self addSubview:_tableView]; | |
[_tableView reloadData]; | |
self.clipsToBounds=YES; | |
} | |
return self; | |
} | |
-(void)addItem:(NSString*)item | |
{ | |
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items]; | |
[tempArray addObject:item]; | |
self->items=[NSArray arrayWithArray:tempArray]; | |
self->numberOfObjects=[self->items count]; | |
} | |
-(void)insertItem:(NSString*)item AtIndex:(int)index | |
{ | |
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items]; | |
[tempArray insertObject:item atIndex:index]; | |
self->items=[NSArray arrayWithArray:tempArray]; | |
self->numberOfObjects=[self->items count]; | |
} | |
-(int)removeItem | |
{ | |
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items]; | |
[tempArray removeLastObject]; | |
self->items=[NSArray arrayWithArray:tempArray]; | |
self->numberOfObjects=[self->items count]; | |
return self->numberOfObjects; | |
} | |
-(void)removeItemAtIndex:(NSUInteger)index | |
{ | |
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items]; | |
[tempArray removeObjectAtIndex:index]; | |
self->items=[NSArray arrayWithArray:tempArray]; | |
self->numberOfObjects=[self->items count]; | |
} | |
-(void)refresh | |
{ | |
[_tableView reloadData]; | |
[self setNeedsDisplay]; | |
} | |
// Only override drawRect: if you perform custom drawing. | |
// An empty implementation adversely affects performance during animation. | |
- (void)drawRect:(CGRect)rect | |
{ | |
[_tableView setNeedsDisplay]; | |
[_textField setNeedsDisplay]; | |
} | |
#pragma mark- Combo Animation | |
-(void)open | |
{ | |
__block CGRect rect=self.frame; | |
rect.size.height=150; | |
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ | |
[self setFrame:rect]; | |
[arrow setTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(270))]; | |
} completion:^(BOOL finished){ | |
[self setNeedsDisplay]; | |
[_tableView reloadData]; | |
}]; | |
} | |
-(void)close | |
{ | |
__block CGRect rect=self.frame; | |
rect.size.height=_textField.frame.size.height; | |
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ | |
[self setFrame:rect]; | |
[arrow setTransform:CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-270))]; | |
} completion:^(BOOL finished) { | |
[self setNeedsDisplay]; | |
}]; | |
} | |
#pragma mark- UITableView datasource methods | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
return [self->items count]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *cellIdentifier=@"cellIdentifier"; | |
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
if (cell==nil) { | |
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; | |
} | |
cell.textLabel.font=[UIFont systemFontOfSize:14.0f]; | |
cell.textLabel.text=[self->items objectAtIndex:indexPath.row]; | |
CGRect cellFrame=cell.frame; | |
cellFrame.size.height=25; | |
[cell setFrame:cellFrame]; | |
UIView *bgView=[[UIView alloc] initWithFrame:cell.frame]; | |
CAGradientLayer *gradient = [CAGradientLayer layer]; | |
gradient.frame = bgView.bounds; | |
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor lightGrayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; | |
[bgView.layer insertSublayer:gradient atIndex:0]; | |
[cell setBackgroundView:bgView]; | |
return cell; | |
} | |
#pragma mark- UITableView delegate methods | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
_textField.text=[self->items objectAtIndex:indexPath.row]; | |
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES]; | |
[self close]; | |
} | |
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate | |
{ | |
[self bringSubviewToFront:_textField]; | |
[self setNeedsDisplay]; | |
} | |
#pragma mark- UITextField delegate method | |
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField | |
{ | |
return NO; | |
} | |
- (void)dealloc | |
{ | |
_tableView.delegate=nil; | |
_tableView.dataSource=nil; | |
[_tableView removeFromSuperview]; | |
_tableView=nil; | |
[arrow removeFromSuperview]; | |
arrow=nil; | |
[_textField removeFromSuperview]; | |
_textField=nil; | |
} | |
@end |
Using Code:-

NSArray *itemArray=[[NSArray alloc] initWithObjects:@"BMW",@"Ferrari",@"Swift Desire",nil];
UICombo *combo=[[UICombo alloc] initWithFrame:CGRectMake(10, 30, 200, 30) andItems:itemArray andColor:CBBrown];
and At Last add it as subview to your view on which you want to display it.
[self.view addSubview:combo];
These combo includes methods to addItem , InsertItemAtIndex,removeItem,removeItemAtIndex,
refresh combo etc.
Note:- Include QuartzCore framework.
You can have a sample code IOSCombo