导航栏的按钮,右边的按钮是可以自己随意添加的。但左边的返回按钮怎么定制?你会说,添加一个自己的按钮呗!你可以试试看,这样行不行。
正确的答案是重载UINavigationController类的pushViewController:animated方法。
#import <UIKit/UIKit.h>
@interface MyNavigationController: UINavigationController {
}
@end
#import "MyNavigationController.h"
@implementation MyNavigationController
-(void)popself
{
}
-(UIBarButtonItem*) createBackButton
{
return [[UIBarButtonItem alloc]
initWithTitle:@"返回"
style:UIBarButtonItemStyleBord
target:self
action:@selector(popself)];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) {
}
@end
使用MyNavigationController替换UINavigationController。或者直接创建一个UINavigationController的新类别
不过这个只会影响当前的UIViewController,即当navigationController导航该UIViewController时,显示的回退才会是“返回”,导般其它的还是会是上一个view controller的title名称~!
使用pushViewController切换到下一个视图时,navigation controller按照以下3条顺序更改导航栏的左侧按钮。
1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;
2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;
3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题。
把UIBarButtonItem *backItem……这段代码放在A视图的pushViewController语句之前。
B视图的后退按钮的标题变成back了。
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"back"style:UIBarButtonItemStyleBord ered target:nil action:nil]; [self.navigationItem setBackBarButtonItem:backItem]; [backItem release]; [self.navigationController pushViewController:self.bView animated:YES];
1.UINavigationController是用于构建分层应用程序的主要工具,它维护了一个视图控制器栈,任何类型的视图控制器都可以放入.
2. 根控制器,子控制器.
3. 导航按钮.
4. 其它术语:
5.静态使用UINavigationController
6.控制器与其成员间的关系
(1).UINavigationController
主要成员:
(2).UINavigationBar管理了一组 UINavigationItem.
(4).UITabBar管理了一组 UITabBarItem.
(5).UIViewController提供了基础的view管理模型.
(6).UINavigationItem是一个视图顶部上的导航条
(7).UIBarButtonItem封装了一些属性和行为,它被添加到UIToolBar中.
7.一些高级控制器
8.http://blog.zhaojie.me/2010/12/iphone-composition-resistant-uitabbarcontroller.html
9.在UIViewController中访问导航:
10.UINavigationItem:
12.在UIViewController中,使用self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f];改变导航栏的颜色,它会形成以中间向上下两边的渐变。其它颜色值不确定能否形成这样的效果。
13.可以继承出一个UINavigationController的子类 ,重写pushViewController,popViewControllerAnimate
14.presentModalViewControll
15.可以使用比标题栏高的背景图片,在UINavigationBar的drawrect中,把它向下延伸一点画出来,可以达到最下边有层半透明的效果。
16.加下阴影
17.如果在push后立即pop,若使用了animited,则要延0.4秒再pop,否则可能会导航变为了上一层的,而内容还是本层的。如果animited为NO,则可以立即pop。
navigation.navigationBar.tintColor
添加一个按钮:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UITabBarSystemItemContac
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
右面添加两个按钮:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 45)];
[tools setTintColor:[self.navigationController.navigationBar tintColor]];
[tools setAlpha:[self.navigationController.navigationBar alpha]];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemI
UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UITabBarSystemItemContac
[buttons addObject:anotherButton];
[anotherButton release];
[buttons addObject:anotherButton1];
[anotherButton1 release];
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = myBtn;
[myBtn release];
[tools release];