iOS编程如何在导航条上创建上下文按钮菜单
2017-04-14 01:56
344 查看
有时候在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];
}
- (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];
}
相关文章推荐
- 如何在其他程序的窗口上创建按钮并使之能响应
- 如何在IE右键菜单中添加菜单项以及如何添加IE任务栏按钮
- 如何在其他程序的窗口上创建按钮并使之能响应
- 使用Jquery+CSS如何创建流动导航菜单-Fluid Navigation
- 使用Jquery+CSS如何创建流动导航菜单-Fluid Navigation 推荐
- MSDN首页菜单导航条,有阴影效果的按钮。分析。
- Symbian(Create sub menu)如何创建二级菜单
- 【转贴】如何利用CSS创建一个漂亮的按钮
- 如何在IE右键菜单中添加菜单项以及如何添加IE任务栏按钮
- 关于如何动态创建按钮控件。
- [转]Creating Custom Web Controls in C# Stats(演示了如何创建一个导航条)
- 如何应用ul、li标签 创建css横向导航菜单?
- 为opera创建代理切换菜单、按钮
- SIF如何创建一个安全上下文学习
- 如何一个创建三态下压按钮
- 教你如何用TreeView树形菜单创建像CSDN左边那样的导航之一:如何安装配置TreeView
- 使用Jquery+CSS如何创建流动导航菜单-Fluid Navigation
- 如何在程序中调用其它程序(如Windows扫雷游戏)时,隐藏它的标题、菜单、工具按钮部分?
- 如何用wgui控件创建菜单
- 如何创建右键弹出菜单(即ContextMenu)