我看到有许多人在项目的时候被下拉菜单所难住,上网上找各种三方,其实它的实现非常的简单,下面我先给大家带来比较简单的下拉菜单,这个是加在导航栏上的
#import "ViewController.h"
#import "ViewButton.h"
@interface ViewController ()
@property (nonatomic ,retain)UIImageView *myView;
@property (nonatomic ,assign)BOOL isDid;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"点击" style:UIBarButtonItemStylePlain target:self action:@selector(Action)];
//下拉菜单的背景
self.myView = [[UIImageView alloc]initWithFrame:CGRectMake(250, 0, 120, 125)];
self.myView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.myView];
ViewButton *btn1 = [ViewButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(0, 0, 120, 40);
btn1.backgroundColor = [UIColor blackColor];
[btn1 addTarget:self action:@selector(btn1Action:) forControlEvents:UIControlEventTouchUpInside];
[self.myView addSubview:btn1];
ViewButton *btn2 = [ViewButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(0, 40, 120, 40);
btn2.backgroundColor = [UIColor orangeColor];
[btn2 addTarget:self action:@selector(btn2Action:) forControlEvents:UIControlEventTouchUpInside];
[self.myView addSubview:btn2];
ViewButton *btn3 = [ViewButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(0, 80, 120, 40);
btn3.backgroundColor = [UIColor lightGrayColor];
[btn3 addTarget:self action:@selector(btn3Action:) forControlEvents:UIControlEventTouchUpInside];
[self.myView addSubview:btn3];
//用户交互
self.myView.userInteractionEnabled = YES;
//默认隐藏菜单
self.myView.hidden =YES;
//设置标签属性
self.isDid = YES;
}
- (void)Action{
if (self.isDid ) {
self.myView.hidden = NO;
self.isDid = NO;
}else{
self.myView.hidden = YES;
self.isDid = YES;
}
}
到这里,简单的下拉菜单就实现了,大家可以根据这个在加些自己所需要的功能.