同理与UITabBarController,一些普遍性的操作就不提到了,大家可以参考下(http://blog.csdn.net/totogo2010/article/details/7681879)的博客。
但是下面这幅图片还是得引用下:
根据网上的一些资料,自己做了一个demo,先上效果
以上的效果是很丑的(只是表达一个意思)。
首先使用到了MainViewController,FirstViewController两个视图。
先创建FirstViewController视图。
/// MainViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//创建导航
FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
viewController.title =@"firstViewController";
UINavigationController *navigationBar = [[UINavigationController alloc] init];
self.navigationController = navigationBar;
[navigationBar pushViewController:viewController animated:YES];
[self.view addSubview:navigationBar.view];
[navigationBar release];
}1.NavigationBar的使用
首先在 MainViewController.h文件中
@property(strong,nonatomic)UINavigationController *navigationController;
在MainViewController.m 中
//1.设置navigationBar的文本颜色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//2.设置navigationBar的背景图片
UIImage *navBarImage = [UIImage imageNamed:@"bottomBK.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:navBarImage];
[self.navigationController.navigationBar addSubview:imageView];
// 3.自定义 设置navigationBar的背景图片 的方式(采用类目的方式)
参考 http://blog.csdn.net/zhuzhihai1988/article/details/7705308
在FirstViewController.m文件中
// 1.上面导航栏中的 左边按钮(系统)
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:@selector(leftButtonAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
//2.上面导航栏中的 自定义左边按钮
UIBarButtonItem *coutomButton = [[UIBarButtonItem alloc] initWithTitle:@"asdfasdf" style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction1:)];
UIButton *button11 = [UIButton buttonWithType:UIButtonTypeCustom];
[button11 setFrame:CGRectMake(0, 0, 40, 30)];
[button11 setBackgroundColor:[UIColor redColor]];
[button11 setBackgroundImage:[UIImage imageNamed:@"102.png"] forState:UIControlStateNormal];
[button11 setBackgroundImage:[UIImage imageNamed:@"102-sel.png"] forState:UIControlStateHighlighted];
coutomButton.customView = button11;
[button11 release];
self.navigationItem.leftBarButtonItem = coutomButton;
[coutomButton release];
//3.自定义中间titleView
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
titleButton.frame = CGRectMake(0, 0, 40, 30);
[titleButton setBackgroundColor:[UIColor whiteColor]];
[titleButton setTitle:@"titlView" forState:UIControlStateNormal];
self.navigationItem.titleView = titleButton;
[titleButton release];
// 4.上面导航栏中的右边按钮(系统)
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(rightButtonAction1:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:rightButton, nil];
[rightButton release];
注意事项:
Navigationbar 的位置坐标和宽度,高度是修改不了的。 即bar.frame = CGRectMake(0,20,self.view.bounds.size.width,30);重新设置navigationbar是不起作用的。
2.NavigationToolBar的使用
在MainViewController.m文件中//1.显示底部工具栏
self.navigationController.toolbarHidden = NO;
//2.设置底部工具栏的背景图片
UIImage *navBarImage11 = [UIImage imageNamed:@"bottomBK.png"];
UIImageView *imageView11 = [[UIImageView alloc] initWithImage:navBarImage11];
[self.navigationController.toolbar addSubview:imageView11];
//3.设置底部工具栏的文本颜色
self.navigationController.toolbar.tintColor = [UIColor redColor];
//4.两个自定义tool下的按钮和系统save按钮
UIBarButtonItem *custom = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];
UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[button3 setBackgroundImage:[UIImage imageNamed:@"102.png"] forState:UIControlStateNormal];
custom.customView = button3;
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
UIButton *button4 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[button4 setBackgroundImage:[UIImage imageNamed:@"102.png"] forState:UIControlStateNormal];
add.customView = button4;
UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];
UIBarButtonItem *save3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];
UIBarButtonItem *save1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];
UIBarButtonItem *save2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];
self.toolbarItems = [NSArray arrayWithObjects:custom,add,save,save1,save2,save3,nil];
[button3 release];
[button4 release];
[custom release];
[add release];
[save release];
[save1 release];
[save2 release];
[save3 release];
注意事项:
NavigationToolbar 的位置坐标和宽度同NavigationBar的一样不能修改的。