iOS编程如何在导航条上创建上下文按钮菜单

      有时候在iOS编程中,需要在指定界面的导航条上创建用用自己的上下文菜单,如:应用分享,设置菜单。需要以代码形式灵活构建相应按钮和事件。先看代码:

       

- (void)createMenuButton:(NSString *)imageName {

    // init your custom button, or your custom view

   

   UIButton*  menuButton = [UIButton buttonWithType:UIButtonTypeCustom];

//定义和构建按钮类型


    menuButton.frame = CGRectMake(baseWidth-40, 0, 30, 30); // custom frame

//创建菜单位置和大小  


    [menuButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];//根据输入的图像资源,创建菜单图像

    [menuButton addTarget:self action:@selector(menuButtonAction:) forControlEvents:UIControlEventTouchUpInside];//定义按钮事件

    

    // set left barButtonItem with custom view

   

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];//分配导航条rightBarButtonItem空间,并初始化为使用者定义的按钮

}

//经过上面的步骤,在应用界面导航条会显示上下文按钮菜单。如果要实现按钮还需要添加相应的事件函数,代码如下:


-(IBAction)menuButtonAction:(id)sender{

    UIAlertController * viewController=   [UIAlertController

                                           alertControllerWithTitle:@"Menus"

                                           message:nil

                                           preferredStyle:UIAlertControllerStyleAlert];

    //UIAlertControllerStyleActionSheet

    viewController.preferredContentSize=CGSizeMake(100, 60);

    

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

    

   

    UIAlertAction* shareAction = [UIAlertAction

                                  actionWithTitle:@"Share with..."

                                  style:UIAlertActionStyleDefault

                                  handler:^(UIAlertAction * action)

                                  {

                                      [self shareInfor];

                                      

                                  }];

    UIAlertAction* loadHelpBox = [UIAlertAction

                                  actionWithTitle:@"Help"

                                  style:UIAlertActionStyleDefault

                                  handler:^(UIAlertAction * action)

                                  {

                                      //Do some thing here

                                      helpType=HELP_OPERATION;

                                      [self performSegueWithIdentifier:@"detail_help_view"sender:self];

                                      NSLog(@"\nloadSettingsBox");

                                      

                                  }];

    

    

    UIAlertAction* loadDefaultSettingBox = [UIAlertAction

                                            actionWithTitle:@"DefaultSettings"

                                            style:UIAlertActionStyleDefault

                                            handler:^(UIAlertAction * action)

                                            {

                                                //Do some thing here

                                                NSLog(@"\nloadDefaultSettingsBox");

                                                [self loadDefaultSettingBox];

                                            }];

   

    [shareAction setValue:[[UIImage imageNamed:@"share.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

    [loadHelpBox setValue:[[UIImage imageNamed:@"help.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

    [loadDefaultSettingBox setValue:[[UIImage imageNamed:@"defaultSettings.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

   

    

    [cancelAction setValue:[[UIImage imageNamed:@"cancel.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

    //[viewController addAction:loadSettingBox];

    [viewController addAction:shareAction];

    [viewController addAction:loadHelpBox];

    [viewController addAction:loadDefaultSettingBox];

    //[viewController addAction:loadRegLogBox];

    [viewController addAction:cancelAction];

    [self presentViewController:viewController animated:YES completion:nil];

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值