Here I am going to explain How to create drop-down menu in iOS like Android.
Step 1)Copy Below two classes
NIDropDown.h
#import <UIKit/UIKit.h>
@class NIDropDown;
@protocol NIDropDownDelegate
- (void) niDropDownDelegateMethod: (NIDropDown *) sender;
@end
@interface NIDropDown : UIView <UITableViewDelegate, UITableViewDataSource>
{
NSString *animationDirection;
}
@property (nonatomic, retain) id <NIDropDownDelegate> delegate;
@property (nonatomic, retain) NSString *animationDirection;
-(void)hideDropDown:(UITextField *)b;
-(id)showDropDown:(UITextField *)b :(CGFloat *)height :(NSArray *)arr :(NSString *)direction;
@end
NIDropDown.m
#import "NIDropDown.h"
#import "QuartzCore/QuartzCore.h"
@interface NIDropDown ()
@property(nonatomic, strong) UITableView *table;
@property(nonatomic, strong) UITextField *textSender;
@property(nonatomic, retain) NSArray *list;
@end
@implementation NIDropDown
@synthesize table;
@synthesize textSender;
@synthesize list;
@synthesize delegate;
@synthesize animationDirection;
- (id)showDropDown:(UITextField *)b :(CGFloat *)height :(NSArray *)arr :(NSString *)direction {
textSender = b;
animationDirection = direction;
self.table = (UITableView *)[super init];
if (self) {
// Initialization code
CGRect btn = b.frame;
self.list = [NSArray arrayWithArray:arr];
if ([direction isEqualToString:@"up"])
{
self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
self.layer.shadowOffset = CGSizeMake(-5, -5);
}
else if ([direction isEqualToString:@"down"])
{
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
self.layer.shadowOffset = CGSizeMake(-5, 5);
}
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 8;
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = 0.5;
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
table.delegate = self;
table.dataSource = self;
table.layer.cornerRadius = 5;
table.backgroundColor = [UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
table.separatorColor = [UIColor grayColor];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
if ([direction isEqualToString:@"up"])
{
self.frame = CGRectMake(btn.origin.x, btn.origin.y-*height, btn.size.width, *height);
}
else if([direction isEqualToString:@"down"])
{
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, *height);
}
table.frame = CGRectMake(0, 0, btn.size.width, *height);
[UIView commitAnimations];
[b.superview addSubview:self];
[self addSubview:table];
}
return self;
}
-(void)hideDropDown:(UIButton *)b
{
CGRect btn = b.frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
if ([animationDirection isEqualToString:@"up"])
{
self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
}
else if ([animationDirection isEqualToString:@"down"])
{
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
}
table.frame = CGRectMake(0, 0, btn.size.width, 0);
[UIView commitAnimations];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
cell.textLabel.text =[list objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
UIView * v = [[UIView alloc] init];
v.backgroundColor = [UIColor grayColor];
cell.selectedBackgroundView = v;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self hideDropDown: textSender];
UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
[textSender setText:c.textLabel.text];
[self myDelegate];
}
- (void) myDelegate
{
[self.delegate niDropDownDelegateMethod:self];
}
-(void)dealloc
{
}
@end
For more info click iOS Drop-down like android Part-2

No comments:
Post a Comment