iOS导航栏创建
来源:互联网 发布:奶茶网络营销策划书 编辑:程序博客网 时间:2024/05/19 14:19
本文是使用纯代码实现一个导航栏的效果。单击按钮并且产生事件。基本思路是:
1.创建一个导航栏(UINavigationBar对象)
2.创建一个导航栏集合(UINavigationItem对象)
3.创建一个左边按钮、一个右边按钮(UIBarButtonItem对象),并实现对应的事件方法
4.将导航栏集合添加到导航栏中,设置动画关闭
5.把左右两个按钮添加到导航栏集合中去
6.在视图中显示当前创建的导航栏
具体的实现代码如下:
ViewController.h文件中的代码不用改变,如下所示:
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController
- @end
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end
ViewController.m文件中的代码:
- #import “ViewController.h”
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- //创建一个导航栏
- UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
- //创建一个导航栏集合
- UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];
- //在这个集合Item中添加标题,按钮
- //style:设置按钮的风格,一共有三种选择
- //action:@selector:设置按钮的点击事件
- //创建一个左边按钮
- UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@”左边” style:UIBarButtonItemStyleBordered target:self action:@selector(clickLeftButton)];
- //创建一个右边按钮
- UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@”右边” style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];
- //设置导航栏的内容
- [navItem setTitle:@”凌凌漆”];
- //把导航栏集合添加到导航栏中,设置动画关闭
- [navBar pushNavigationItem:navItem animated:NO];
- //把左右两个按钮添加到导航栏集合中去
- [navItem setLeftBarButtonItem:leftButton];
- [navItem setRightBarButtonItem:rightButton];
- //将标题栏中的内容全部添加到主视图当中
- [self.view addSubview:navBar];
- //最后将控件在内存中释放掉,以避免内存泄露
- [navItem release];
- [leftButton release];
- [rightButton release];
- }
- -(void)showDialog:(NSString *)str
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”这是一个对话框” message:str delegate:self cancelButtonTitle:@“确定” otherButtonTitles: nil];
- [alert show];
- [alert release];
- }
- -(void) clickLeftButton
- {
- [self showDialog:@”点击了导航栏左边按钮”];
- }
- -(void) clickRightButton
- {
- [self showDialog:@”点击了导航栏右边按钮”];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- }
- @end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //创建一个导航栏 UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; //创建一个导航栏集合 UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil]; //在这个集合Item中添加标题,按钮 //style:设置按钮的风格,一共有三种选择 //action:@selector:设置按钮的点击事件 //创建一个左边按钮 UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStyleBordered target:self action:@selector(clickLeftButton)]; //创建一个右边按钮 UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)]; //设置导航栏的内容 [navItem setTitle:@"凌凌漆"]; //把导航栏集合添加到导航栏中,设置动画关闭 [navBar pushNavigationItem:navItem animated:NO]; //把左右两个按钮添加到导航栏集合中去 [navItem setLeftBarButtonItem:leftButton]; [navItem setRightBarButtonItem:rightButton]; //将标题栏中的内容全部添加到主视图当中 [self.view addSubview:navBar]; //最后将控件在内存中释放掉,以避免内存泄露 [navItem release]; [leftButton release]; [rightButton release];}-(void)showDialog:(NSString *)str{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show]; [alert release];}-(void) clickLeftButton{ [self showDialog:@"点击了导航栏左边按钮"];}-(void) clickRightButton{ [self showDialog:@"点击了导航栏右边按钮"];}- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}@end
阅读全文
0 0
- iOS导航栏创建
- IOS TabBarController创建导航栏
- iOS 导航栏创建多个Button
- IOS-导航风格:BarButton创建
- IOS-导航风格:BarButton创建
- IOS导航,tabBar快速创建
- IOS-导航风格:BarButton创建
- IOS学习 IOS-导航风格:BarButton创建
- 导航栏的创建
- iOS 创建一个滑出式导航面板
- ios 导航控制器(navigationController)代码方式创建
- IOS导航栏UINavigation
- ios 导航栏风格
- ios 导航栏显示
- IOS-导航栏风格
- ios 导航栏设置
- iOS 导航栏
- ios顶部导航栏
- 【React】配置react-hot-loader后出现import' and 'export' may only appear at the top level
- Effective C++笔记
- 521356
- Qt5--文本编辑器 (二)
- node核心模块之path
- iOS导航栏创建
- Django Model
- 各种正则:邮箱,电话号码,身份证号。
- mac版本idea使用(四)
- Android编程权威指南(第2版)—第2章挑战练习
- H5 video标签在移动端,不默认全屏播放。
- vue开发——如可操作文档元素
- mysql1577错误
- 解决jenkins插件列表为空的问题