導(dǎo)航條更改

1,創(chuàng)建并使用一個UINavigationController UINavigationController *aNav = [[UINavigationController alloc] init];然后添加一個視圖進去,否則導(dǎo)航欄也沒有意義的 UIViewController *aViewCtrl = [[UIView alloc] initWithNibName: (*xib文件名*)];[aNav pushViewController:aViewCtrl animated:NO];//導(dǎo)航欄的第一個視圖不要動畫化aViewCtrl.title = @"標題"; //設(shè)置其標題:2,設(shè)置導(dǎo)航欄的左右按鈕:設(shè)置導(dǎo)航欄的按鈕并不是去設(shè)置導(dǎo)航欄本身,而是當(dāng)時被導(dǎo)航的視圖控制器,比如我們對aView作設(shè)置。//配置一個按鈕,我這里是《我的佛典》上的代碼UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc] initWithTitle:@"經(jīng)文"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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:這個方法根本沒有調(diào)用,后來發(fā)現(xiàn)原來用了UINavigationController后,viewWillAppear方法是沒有效果的,要用UINavigationControllerDelegate的– navigationController:willShowViewController:animated:方法才可以達到這個目的。所以要做到這個,你必須做以下幾步:1. 設(shè)置代理類nav.delegate = self; 2. 代理類實現(xiàn)UINavigationControllerDelegate Protocol3. 在代理類中添加– navigationController:willShowViewController:animated:方法如:- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ [self.myTableView reloadData];}pushViewController:viewController animated:BOOL(加載視圖控制器)– 添加指定的視圖控制器并予以顯示,后接:是否動畫顯示popViewControllerAnimated:BOOL(彈出當(dāng)前視圖控制器)– 彈出并向左顯示前一個視圖popToViewController:viewController animated:BOOL(彈出到指定視圖控制器)– 回到指定視圖控制器, 也就是不只彈出一個popToRootViewControllerAnimated:BOOL(彈出到根視圖控制器)– 比如說你有一個“Home”鍵,也許就會實施這個方法了。setNavigationBarHidden:BOOL animated:BOOL(設(shè)置導(dǎo)航欄是否顯示)– 如果你想隱藏導(dǎo)航欄,這就是地方了。參照Picasa的WebApp樣式,現(xiàn)pushViewController:animated:的不同頁面轉(zhuǎn)換特效1. 首先要明確的是,不使用pushViewController的默認動畫,所以在調(diào)用這個函數(shù)時,要將animated設(shè)置為NO.2. 使用普通的來CATransition實現(xiàn)轉(zhuǎn)換效果,代碼如下: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];經(jīng)常要在導(dǎo)航欄中添加各種樣式的按鈕,添加一個按鈕很簡單,代碼如下圖: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/在有些項目中要在右面添加兩個按鈕,實現(xiàn)的樣式如下圖:image 實現(xiàn)的代碼如下圖:UIToolbar* tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 45)];//使用一個容器去裝載多個UIBarButtonItem是實現(xiàn)多個按鈕的最核心的部分[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];隱藏當(dāng)前頁的navigationBar:? ? [self.navigationController setNavigationBarHidden:YES animated:YES]; 給 UINavigationBar 設(shè)置背景圖片方法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;}調(diào)用的時候: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 {//加入旋轉(zhuǎn)坐標系代碼? // 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自定義導(dǎo)航欄背景嗎?查了很多資料,網(wǎng)上的自定義導(dǎo)航欄的方法,清一色的是擴展navigationBar的drawRect方法.然而這樣的擴展會影響到工程里所有的navigationBar.或許你并不想這么做,而且很多不合常規(guī)的UI用這種方法根本沒法實現(xiàn).做了無數(shù)實驗,嘗試了各種方法,今天跟大家分享一個新方法:#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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • //設(shè)置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 838評論 0 0
  • 1,Search Bar 怎樣去掉背景的顏色(storyboard里只能設(shè)置background顏色,可是發(fā)現(xiàn)cl...
    以德扶人閱讀 2,485評論 2 50
  • 1.自定義控件 a.繼承某個控件 b.重寫initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,484評論 2 4
  • 薊縣農(nóng)家院,我們又來了,我們喜歡這里種的花草,喜歡這里種的蔬果,更喜歡這里的人實在熱情。所以每年夏天都想來這看看您...
    森林羊羊閱讀 482評論 0 0
  • 這一周基本上比較放松,沒怎么學(xué)習(xí)。因為好友結(jié)婚的關(guān)系,所以很多時間都安排在旅程上。 計劃安排 這周很自覺準備了手賬...
    源源噠閱讀 267評論 0 1