UINavigationBar & UINavigationItem
2015-08-11 22:17
274 查看
如果在一个UINavigationController容器类里面push一个UIViewController的话,nagationBarItem是很好处理的,因为root根窗口提供了一个导航栏,用个自定义的样式按钮可以直接去替换它,并且iOS提供了丰富的使用API去使用。
但是如果在一个UITabBarController(非UINavigationController类)里面直接初始化一个UIViewController并用push它,达不到push的美妙效果(很显然),更令人头痛的是,连导航栏都是空的,那功能十分好用的导航栏按钮就更是谈不上了。
这个时候我们可以手动添加UIViewController的navigationBar(只是加上了导航栏,push问题仍然解决不了哈)
如果想将UIBarButtonItem设置成自定义的,可以:
第一种方法:用图片上填充
第二种方法:自定义UIButton来替代
第三种方法:用系统提供的样式
如果想要它隐藏呢:
参考至:
超详细总结:http://blog.csdn.net/zhibudefeng/article/details/7656311
添加导航栏:http://blog.sina.com.cn/s/blog_8f5097be0101ath7.html
修改barButton: http://blog.csdn.net/zhuzhihai1988/article/details/7701998
但是如果在一个UITabBarController(非UINavigationController类)里面直接初始化一个UIViewController并用push它,达不到push的美妙效果(很显然),更令人头痛的是,连导航栏都是空的,那功能十分好用的导航栏按钮就更是谈不上了。
这个时候我们可以手动添加UIViewController的navigationBar(只是加上了导航栏,push问题仍然解决不了哈)
//添加了导航栏 CGRect rect = [[UIScreen mainScreen] bounds]; UINavigationBar* navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 64)]; //修改tint颜色,方法是宏定义的 navigationBar.tintColor = UIColorWithRGB(200, 100, 162); //设置NavigationItem UINavigationItem* navigationBarTitle = [[UINavigationItem alloc] initWithTitle:@"Delegation Test"]; [navigationBar pushNavigationItem:navigationBarTitle animated:YES]; [self.view addSubview:navigationBar]; //设置了系统的UIBarButtonItem样式 UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(navigationItemCancel:)]; navigationBarTitle.leftBarButtonItem = item; [navigationBar setItems:[NSArray arrayWithObject:navigationBarTitle]];
如果想将UIBarButtonItem设置成自定义的,可以:
第一种方法:用图片上填充
UIImage* searchimage = [UIImage imageNamed:@"search.png"]; UIBarButtonItem* barbtn = [[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(searchprogram)]; barbtn.image = searchimage; self.navigationItem.rightBarButtonItem = barbtn;
第二种方法:自定义UIButton来替代
UIButton* rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)]; [rightButton setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal]; [rightButtonaddTarget:self action:@selector(searchprogram) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; self.navigationItem.rightBarButtonItem= rightItem;
第三种方法:用系统提供的样式
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(methodtocall:) ];
如果想要它隐藏呢:
self.navigationItem.rightBarButtonItem = nil;
参考至:
超详细总结:http://blog.csdn.net/zhibudefeng/article/details/7656311
添加导航栏:http://blog.sina.com.cn/s/blog_8f5097be0101ath7.html
修改barButton: http://blog.csdn.net/zhuzhihai1988/article/details/7701998
相关文章推荐
- 视图UIView的frame、bounds与center属性
- LeetCode(187) Repeated DNA Sequences
- request.getParameterValues与request.getParameter的区别
- request.getParameterValues与request.getParameter的区别
- UIAutomation调用计算器模拟自动执行
- iOS UILocalNotification定时提醒
- 更新UI方式
- Divide and Conquer.(Merge Sort) by sixleaves
- Handler与looper、MessageQueue的关系
- *** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit
- hdu5064 Find Sequence 单调性dp
- 自定义iOS UIpagecontrol 小圆点 换上自定义图片
- iOS基础-UIKit框架-高级视图-UIPickerView-实例3:国家选择(图片)
- UITableView(基本设置和常用的协议方法)
- UITableView的编辑
- 窗口与视图UIScreen and UIView
- Blue Jeans(POJ--3080
- 2015 Multi-University Training Contest 6 (hdu 5357 - Easy Sequence)栈的应用
- 深入理解urllib、urllib2及requests
- UITableView 隐藏没有数据线条