iOS 下拉菜单

这篇博客介绍了如何在iOS项目中实现一个简单的下拉菜单,该菜单附加在导航栏上。通过创建UIImageView作为背景,并添加多个ViewButton作为选项,利用UIButton的点击事件实现菜单的显示与隐藏。代码示例详细展示了具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 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 {

    [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;

        

        

    }

}


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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值