自定义 UINavigationItem 的简单实现

时间:2021-09-09 16:10:16

所有的实现都在UInavigionController的rootViewController中实现。

        导航控制器维护一个视图控制器栈,任何类型的视图控制器都可以放入栈中。在设计导航器时,需要指定用户看到的第一个视图。该视图是层次结构中最底层的视图,其控制器称为根视图控制器。在根视图控制器的视图上方是UINavigationItem 控件。这个控件是根控制器入栈后,它也随之进入UINavigationBar的栈,并作为栈顶,它代表着当前导航器的状态。

          UITabBarController   和 UInavigionController一般作为应用程序的主控制器被加载,但他们都要设置根视图控制器去显示根视图控制器的view,让其加载在他们的view上。

         

//
// ViewController.m
// NavigationBarMuliBars
//
// Created by apple on 12-7-5.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize titleLabel;
@synthesize titleButton;
@synthesize indicator;
@synthesize leftButton;
@synthesize testButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

//设置导航栏标题
//self.navigationItem.title = @"题目";

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"navTitleLabel" owner:self options:nil];

if([nib count] > 0)
{
NSLog(@"自定义导航栏标题成功");
}

//自定义label的导航栏标题
//self.navigationItem.titleView = self.titleLabel;

//自定义button的导航栏标题
self.navigationItem.titleView = self.titleButton;

//添加多个右按钮
UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:nil];

UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:nil];

NSArray *buttonArray = [[NSArray alloc] initWithObjects:rightButton1,rightButton2 ,nil];

self.navigationItem.rightBarButtonItems = buttonArray;

[rightButton1 release];
[rightButton2 release];
[buttonArray release];

//设置左按钮
[leftButton addTarget:self action:@selector(requestStart:) forControlEvents:UIControlEventTouchUpInside];
//最重要的一步,注意initWithCustomView方法。理论上左按钮可以是任意父类是UIView的控件.
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];

//设者导航栏的颜色
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.titleLabel = nil;
self.titleButton = nil;
self.leftButton = nil;
self.testButton = nil;
self.indicator = nil;



}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)requestStart:(id)sender
{
[self startIndicator];
//业务逻辑实现
}

- (IBAction)requestStop:(id)sender
{
[self stopIndicator];
//业务逻辑实现
}

- (void)startIndicator
{
self.navigationItem.leftBarButtonItem = nil;
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:indicator];
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];
}

- (void)stopIndicator
{
self.navigationItem.leftBarButtonItem = nil;
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];
}

@end