UINavigationbar的背景修改方法集合
最近我突然發(fā)現(xiàn)UINavigationbar背景修改的方法不起作用了,代碼如下:
| 1 2 3 4 5 6 7 8 9 | @implementation?UINavigationBar?(CustomImage) -(void)drawRect:(CGRect)rect? { ????UIImage?*image?=?[UIImage imageNamed:@"navigationbar.png"]; ????[image drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)]; } @end |
發(fā)現(xiàn)原來(lái)是iOS 5的原因,如果運(yùn)行在iOS 5以下的版本就沒(méi)有問(wèn)題了。經(jīng)過(guò)實(shí)驗(yàn)以下方法適合iOS 5(放在ViewDidLoad中):
| 1 2 3 | if?([self.navigationController.navigationBar respondsToSelector:@selector(?setBackgroundImage:forBarMetrics:)]){ ????????[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"]?forBarMetrics:UIBarMetricsDefault]; ????} |
第一條if語(yǔ)句的作用是防止程序在iOS 5以下的版本中崩潰。
這樣,依靠這兩段代碼,我的UINavigationbar的背景問(wèn)題在iOS 5及以下版本中得到了完美的解決。
?
?
iOS5之前是如何自定義UINavigationBar背景的?
在iOS5.0中我們可以非常簡(jiǎn)單的設(shè)置UINavigationBar的背景(setBackgroundImage: forBarMetrics:方法),而這對(duì)于之前的版本是不可同日而語(yǔ)的。通過(guò)網(wǎng)絡(luò)收集整理了一下以前的各種方式,只能作為學(xué)習(xí)筆記做個(gè)記錄,菜鳥學(xué)習(xí)而已。高人就跳過(guò)吧。
方法一:主要技巧就是用視圖的drawInRect:方法繪制
如下為創(chuàng)建了一個(gè)UINavigationBar Category
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
//顏色填充
UIColor *color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context,CGColorGetComponents([color CGColor]));
CGContextFillRect(context,rect);
self.tintColor = color;
//圖片填充
UIColor *color = [UIColor colorWithRed:46.0f/255.0f green:87.0f/255.0f blue:29.0f/255.0f alpha:1.0f];
UIImage *img = [UIImage imageNamed:@"bg.png"];
[img drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
self.tintColor = color;
}
@end
自定義圖片背景以下兩句代碼是關(guān)鍵:
UIImage *img = [UIImage imageNamed:@"bg.png"];
[img drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
或者:
UIImage *img = [UIImage imageNamed:@"bg.png"];
CGPoint point = {0,0};
[img drawAtPoint:point];
或者:
//加入旋轉(zhuǎn)坐標(biāo)系代碼
?
// Drawing code
?
UIImage?*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);
擴(kuò)展UINavigationBar的drawRect方法的這種自定義方法會(huì)影響到工程項(xiàng)目中所有的導(dǎo)航條欄。
類似在iOS5.0中,由于UINavigationBar、UIToolBar和UITabBar的實(shí)現(xiàn)方式改變,而drawRect:方法不會(huì)被調(diào)用了,所以就不支持這種通過(guò)定義導(dǎo)航條類別的方式來(lái)自定義導(dǎo)航條了。除非在這類控件的子類中實(shí)現(xiàn)。
@interface MyNavigationBar : UINavigationBar
@end
@implementation MyNavigationBar
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
}
@end
方法二:定義UINavigationBar的一個(gè)static函數(shù)
2 UINavigationBar *customNavigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
3 UIImageView *navigationBarBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
4 [customNavigationBar addSubview:navigationBarBackgroundImageView];
5 UINavigationItem *navigationTitle = [[UINavigationItem alloc] initWithTitle:title];
6 [customNavigationBar pushNavigationItem:navigationTitle animated:NO];
7 [navigationTitle release];
8 [navigationBarBackgroundImageView release];
9 return customNavigationBar;
10 } 復(fù)制代碼
下面是在需要生成UINavgationBar 的地方添加的代碼 ????*ViewController.m:
2 UIImage *navigationBarBackgroundImage =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"topbar-bg" ofType:@"png"]];
3 UINavigationBar *customNavigationBar = [YOUR_Util_Class createNavigationBarWithBackgroundImage:navigationBarBackgroundImage title:nil];
4 [self.view addSubview:customNavigationBar];
5
6 UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 75.0, 30.0)];
7 if (_backButtonImage) {
8 [backButton setImage:_backButtonImage forState:UIControlStateNormal];
9 }else {
10 [backButton setImage:[UIImage imageNamed:@"btnback.png"] forState:UIControlStateNormal];
11 }
12
13 [backButton addTarget:self action:@selector(backButtonCliked:) forControlEvents:UIControlEventTouchUpInside];
14 UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
15 customNavigationBar.topItem.leftBarButtonItem = backBarButton;
16
17 [backButton release];
18 [backBarButton release];
19
20 UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 43, 30)];
21 UIBarButtonItem *addBarButton = [[UIBarButtonItem alloc] initWithCustomView:addButton];
22 if (_isFromFavorites) {
23 [addButton setImage:[UIImage imageNamed:@"btn-delete-0.png"] forState:UIControlStateNormal];
24 [addButton addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
25 }else {
26 [addButton setImage:[UIImage imageNamed:@"btn_add.png"] forState:UIControlStateNormal];
27 [addButton addTarget:self action:@selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
28 }
29 customNavigationBar.topItem.rightBarButtonItem = addBarButton;
30 [addButton release];
31 [addBarButton release]; 復(fù)制代碼
此代碼效果圖如下:
這一方法轉(zhuǎn)載自:http://www.cnblogs.com/moshengren/archive/2010/10/18/1855191.html
方法三:也是自定義導(dǎo)航條類別,但是重寫setBackgroundImage:方法
CustomNavController.h
2 // Copyright 2010 __MyCompanyName__. All rights reserved.
3 //
4 #pragma once
5 #import <UIKit/UIKit.h>
6 @interface UINavigationBar (UINavigationBarCategory)
7 UIImageView *bg;
8 -(UINavigationBar*)setBackgroundImage:(UIImage*)image;
9 - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
10 @end 復(fù)制代碼
CustomNavController.m
2
3 @implementation UINavigationBar (UINavigationBarCategory)
4 -(UINavigationBar*)setBackgroundImage:(UIImage*)image
5 {
6 UINavigationBar *NavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
7 if(image == nil) return NavBar;
8 bg = [[UIImageView alloc]initWithImage:image];
9 bg.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
10 [NavBar addSubview:bg];
11 [NavBar sendSubviewToBack:bg];
12 [bg release];
13 return NavBar;
14 }
15
16 - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
17 {
18 [super insertSubview:view atIndex:index];
19 [self sendSubviewToBack:bg];
20 }
21 @end 復(fù)制代碼
調(diào)用代碼示例:
[super viewDidLoad];
[[self.navigationController navigationBar] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground.png"]];
//在下面添加你自己的功能代碼
***********
}
此方法轉(zhuǎn)載自:http://www.cnblogs.com/moshengren/archive/2010/10/18/1855202.html
方法四:通過(guò)導(dǎo)入QuartzCore框架繪制CALayer層來(lái)自定義
@interface DDNavigationViewController : UINavigationController<UINavigationControllerDelegate> {
CALayer *_barBackLayer;
}
@end
?
View Code 1 @implementation DDNavigationViewController2
3 - (id)initWithRootViewController:(UIViewController *)rootViewController {
4
5 self = [super initWithRootViewController:rootViewController];
6
7 self.delegate = self;
8
9 return self;
10
11 }
12
13 - (void)loadView {
14
15 [super loadView];
16
17 UINavigationBar *bar = self.navigationBar;
18
19 CALayer*layer = [CALayer layer];
20
21 UIImage *navBarImage = [UIImage imageNamed:@"navigationBarBackground.png"];
22
23 layer.contents = (id)navBarImage.CGImage;
24
25 layer.frame= CGRectMake(0, 0, 320, navBarImage.size.height);
26
27 [bar.layer insertSublayer:layer atIndex:0];
28
29 _barBackLayer = layer;
30
31 }
32
33
34 #pragma mark -
35
36 #pragma mark UINavigationControllerDelegate
37
38 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
39
40 [_barBackLayer removeFromSuperlayer];
41
42 [navigationController.navigationBar.layer insertSublayer:_barBackLayeratIndex:0];
43
44 }
45
46 @end posted on 2012-07-16 20:23 linyawen 閱讀(...) 評(píng)論(...) 編輯 收藏
轉(zhuǎn)載于:https://www.cnblogs.com/linyawen/archive/2012/07/16/2594131.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的UINavigationbar的背景修改方法集合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于继承方式和访问权限
- 下一篇: Remote PC另类用法:远程控制