1,創建并使用一個UINavigationController UINavigationController *aNav = [[UINavigationController alloc] init];然后添加一個視圖進去,否則導航欄也沒有意義的 UIViewController *aViewCtrl = [[UIView alloc] initWithNibName: (*xib文件名*)];[aNav pushViewController:aViewCtrl animated:NO];//導航欄的第一個視圖不要動畫化aViewCtrl.title = @"標題"; //設置其標題:2,設置導航欄的左右按鈕:設置導航欄的按鈕并不是去設置導航欄本身,而是當時被導航的視圖控制器,比如我們對aView作設置。//配置一個按鈕,我這里是《我的佛典》上的代碼UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc] initWithTitle:@"經文"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? style:UIBarButtonItemStyleBordered target:self action:@selector(callModalList)];self.navigationItem.leftBarButtonItem = callModalViewButton;[callModalViewButton release]; //由于本地視圖會retain它,所以我們可以release了 可以看到,還是很簡單的嘛。3,其他常用方法和屬性:本地視圖.navigationItem.leftBarButtonItem //左邊欄項目本地視圖.navigationItem.rightBarButtonItem //右邊欄項目本地視圖.navigationItem.backBarButtonItem //后退欄項目本地視圖.navigationItem.hidesBackButton //隱藏后退按鈕(YES or NO)在視圖的viewWillAppear:方法中添加:[self.tableView reloadData]; 不起作用,viewWillAppear:這個方法根本沒有調用,后來發現原來用了UINavigationController后,viewWillAppear方法是沒有效果的,要用UINavigationControllerDelegate的– navigationController:willShowViewController:animated:方法才可以達到這個目的。所以要做到這個,你必須做以下幾步:1. 設置代理類nav.delegate = self; 2. 代理類實現UINavigationControllerDelegate Protocol3. 在代理類中添加– navigationController:willShowViewController:animated:方法如:- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ [self.myTableView reloadData];}pushViewController:viewController animated:BOOL(加載視圖控制器)– 添加指定的視圖控制器并予以顯示,后接:是否動畫顯示popViewControllerAnimated:BOOL(彈出當前視圖控制器)– 彈出并向左顯示前一個視圖popToViewController:viewController animated:BOOL(彈出到指定視圖控制器)– 回到指定視圖控制器, 也就是不只彈出一個popToRootViewControllerAnimated:BOOL(彈出到根視圖控制器)– 比如說你有一個“Home”鍵,也許就會實施這個方法了。setNavigationBarHidden:BOOL animated:BOOL(設置導航欄是否顯示)– 如果你想隱藏導航欄,這就是地方了。參照Picasa的WebApp樣式,現pushViewController:animated:的不同頁面轉換特效1. 首先要明確的是,不使用pushViewController的默認動畫,所以在調用這個函數時,要將animated設置為NO.2. 使用普通的來CATransition實現轉換效果,代碼如下:CATransition *animation = [CATransition animation];[animation setDuration:0.3];[animation setType: kCATransitionMoveIn];[animation setSubtype: kCATransitionFromTop];[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];[self.navigationController pushViewController:m_poseAddIssueViewController animated:NO];[self.navigationController.view.layer addAnimation:animation forKey:nil];經常要在導航欄中添加各種樣式的按鈕,添加一個按鈕很簡單,代碼如下圖:UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc]initWithTitle:@"Setting"style:UITabBarSystemItemContacts? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? target:selfaction:@selector(clickSettings:)];? ? ? ? ? self.navigationItem.rightBarButtonItem =anotherButton; [anotherButton release];其中按鈕的樣式可以有多種,具體的可以參考:https://developer.apple.com/library/ios/prerelease/#documentation/UIKit/Reference/UIBarButtonItem_Class/在有些項目中要在右面添加兩個按鈕,實現的樣式如下圖:image 實現的代碼如下圖:UIToolbar* tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 45)];//使用一個容器去裝載多個UIBarButtonItem是實現多個按鈕的最核心的部分[tools setTintColor:[self.navigationController.navigationBartintColor]]; [tools setAlpha:[self.navigationController.navigationBaralpha]]; NSMutableArray* buttons = [[NSMutableArray alloc]initWithCapacity:2];UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd? ? ? ? ? ? ? ? ? ? ? ? target:self action:@selector(clickSettings:)];UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc]initWithTitle:@"Edit"style:UITabBarSystemItemContacts? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? target:self action:@selector(clickEdit:)]; [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];隱藏當前頁的navigationBar:? ? [self.navigationController setNavigationBarHidden:YES animated:YES]; 給 UINavigationBar 設置背景圖片方法1:+ (UINavigationBar *)createNavigationBarWithBackgroundImage:(UIImage *)backgroundImage title:(NSString *)title {? ? UINavigationBar *customNavigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];? ? UIImageView *navigationBarBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];? ? [customNavigationBar addSubview:navigationBarBackgroundImageView];? ? UINavigationItem *navigationTitle = [[UINavigationItem alloc] initWithTitle:title];? ? [customNavigationBar pushNavigationItem:navigationTitle animated:NO];? ? [navigationTitle release];? ? [navigationBarBackgroundImageView release];? ? return customNavigationBar;}調用的時候:self.navigationController.navigationBar.hidden = YES;? ? UIImage *navigationBarBackgroundImage =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"topbar-bg" ofType:@"png"]];? ? UINavigationBar *customNavigationBar = [YOUR_Util_Class createNavigationBarWithBackgroundImage:navigationBarBackgroundImage title:nil];? ? [self.view addSubview:customNavigationBar];方法2:利用objective-c的Category語法擴展UINavigationBar 類具體代碼為@implementation UINavigationBar(UINavigationBarCategory)- (void)drawRect:(CGRect)rect {? ? // Drawingcode UIImage *img = [UIImage imageNamed:@"navbar_background.png"];CGPoint point = {0,0};[img drawAtPoint:point];}@end 方法3:@implementation UINavigationBar(UINavigationBarCategory) - (void)drawRect:(CGRect)rect {//加入旋轉坐標系代碼? // Drawing codeUIImage *navBarImage =[UIImage imageNamed:@"LOGO_320×44.png"];CGContextRef context= UIGraphicsGetCurrentContext();CGContextTranslateCTM(context, 0.0, self.frame.size.height);CGContextScaleCTM(context, 1.0,-1.0);CGPoint center=self.center; CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80,self.frame.size.height),cgImage);CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320,self.frame.size.height),navBarImage.CGImage);CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80,self.frame.size.height),cgImage);}@endold codeCGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width,self.frame.size.height),navBarImage.CGImage);方法4:有了這個,你還會擴展drawRect自定義導航欄背景嗎?查了很多資料,網上的自定義導航欄的方法,清一色的是擴展navigationBar的drawRect方法.然而這樣的擴展會影響到工程里所有的navigationBar.或許你并不想這么做,而且很多不合常規的UI用這種方法根本沒法實現.做了無數實驗,嘗試了各種方法,今天跟大家分享一個新方法:#import@interface DDNavigationViewController: UINavigationController{
CALayer *_barBackLayer;
}
@end
@implementation DDNavigationViewController
- (id)initWithRootViewController:(UIViewController *)rootViewController{
self =[super initWithRootViewController:rootViewController];
self.delegate = self;
return self;
}
- (void)loadView{
[super loadView];
UINavigationBar *bar= self.navigationBar;
CALayer*layer= [CALayer layer];
UIImage *navBarImage= [UIImage imageNamed:@"navigationBarBackground.png"];
layer.contents =(id)navBarImage.CGImage;
layer.frame= CGRectMake(0,0, 320,navBarImage.size.height);
[bar.layer insertSublayer:layer atIndex:0];
_barBackLayer =layer;
}
#pragma mark -
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationControllerdidShowViewController:(UIViewController *)viewControlleranimated:(BOOL)animated{
[_barBackLayer removeFromSuperlayer];
[navigationController.navigationBar.layer insertSublayer:_barBackLayeratIndex:0];
}
@end