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

    xiaoxiao2021-04-15  66

          有时候在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];

    }

    转载请注明原文地址: https://ju.6miu.com/read-671207.html

    最新回复(0)