使用纯代码实现一个导航栏的效果。单击按钮并且产生事件。基本思路是:
1.创建一个导航栏(UINavigationBar对象)
2.创建一个导航栏集合(UINavigationItem对象)
3.创建一个左边按钮、一个右边按钮(UIBarButtonItem对象),并实现对应的事件方法
具体的实现代码如下:
ViewController.h文件
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
ViewController.m文件中的代码:
@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
相关推荐
个人头像可以随手指移动缩放到导航栏。参考了 《仿买单吧APP个人中心导航栏渐变》(http://code.cocoachina.com/view/136112), 下期项目需要,提前造轮子,因为要求布局用masonry,所以自己重新写了代码。 同时代码...
NULL 博文链接:https://dingran.iteye.com/blog/1766186
iOS 导航栏 源代码 一个iOS导航的源程序代码
IOS 导航栏抽屉按钮视图
IOS 自定义导航栏 和表格 学习笔记具体可以参考我的这篇博文http://blog.csdn.net/robinson_911/article/details/42609493
一个类似ios的导航栏滚动效果,适合于企业介绍,新闻浏览等内容时使用。下载后既可运行。
拖动ViewController的时候,导航栏实现颜色渐变
基于IOS的一个侧边导航栏的Demo 使用起来很方便
ios在导航栏的barbuttonitem 点击 出现下拉列表
IOS修改导航栏高度demo,项目非常简单简洁,方便大家参考如何正确地修改UINavigationController中的MyNavigationBar高度,博客地址https://blog.csdn.net/u010775335/article/details/86176848
iOS 图像导航栏刷新 INBPullToRefresh ,INBPullToRefresh 是用于 iOS 上的图像导航栏刷新库。
自定义导航栏,自定义NavigationBar
通过kvc替换系统的导航条,然后再改变导航条背景,需要自定义导航控制器,自定义导航条去实现的。
写了一个小demo,实现了当我滑动控制器的界面,当偏移contentOffSet的y值达到一定时候,会渐渐根据偏移值,来改变导航栏的颜色
可以直接用的iOS导航条
点击导航栏弹窗
mPass iOS 定制导航栏(AUThemeManager)
快速封装导航栏
使用方法: 1、把ET_NavBarTransparent.swift导入到项目中 2、在需要的地方修改导航栏透明度和navigationItem的颜色: override func viewDidLoad() { super.viewDidLoad() self.navBarBgAlpha = 0 ...