iOS 下拉菜单

来源:互联网 发布:java调用weka神经网络 编辑:程序博客网 时间:2024/06/07 20:28

     我看到有许多人在项目的时候被下拉菜单所难住,上网上找各种三方,其实它的实现非常的简单,下面我先给大家带来比较简单的下拉菜单,这个是加在导航栏上的

 viewController.m的代码如下:
这里 ViewButton是一个基于 UIButton 的类 方便自定使用的

#import "ViewController.h"

#import "ViewButton.h"


@interface ViewController ()


@property (nonatomic ,retain)UIImageView *myView;

@property (nonatomic ,assign)BOOL isDid;



@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

    self.navigationController.navigationBar.translucent = NO;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"点击"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(Action)];

    //下拉菜单的背景

    self.myView = [[UIImageViewalloc]initWithFrame:CGRectMake(250,0, 120,125)];

    self.myView.backgroundColor = [UIColorlightGrayColor];

    [self.viewaddSubview:self.myView];

    

    ViewButton *btn1 = [ViewButtonbuttonWithType:UIButtonTypeCustom];

    btn1.frame  = CGRectMake(0, 0, 120, 40);

    btn1.backgroundColor = [UIColorblackColor];

    [btn1 addTarget:selfaction:@selector(btn1Action:)forControlEvents:UIControlEventTouchUpInside];

    [self.myViewaddSubview:btn1];

    

    ViewButton *btn2 = [ViewButtonbuttonWithType:UIButtonTypeCustom];

    btn2.frame  = CGRectMake(0, 40, 120, 40);

    btn2.backgroundColor = [UIColororangeColor];

    [btn2 addTarget:selfaction:@selector(btn2Action:)forControlEvents:UIControlEventTouchUpInside];

    [self.myViewaddSubview:btn2];

    

    ViewButton *btn3 = [ViewButtonbuttonWithType:UIButtonTypeCustom];

    btn3.frame  = CGRectMake(0, 80, 120, 40);

    btn3.backgroundColor = [UIColorlightGrayColor];

    [btn3 addTarget:selfaction:@selector(btn3Action:)forControlEvents:UIControlEventTouchUpInside];

    [self.myViewaddSubview: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;

        

        

    }

}


到这里,简单的下拉菜单就实现了,大家可以根据这个在加些自己所需要的功能.





0 0
原创粉丝点击