<5-1> 自定义Navigation导航的titleView
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(255.0 / 255.0) blue:(0.0 / 255.0) alpha:1];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = @"Custom Title";
self.navigationItem.titleView = titleLabel;
<5-2> 给Navigation Bar设置背景颜色
self.navigationController.navigationBar.tintColor =
[UIColor colorWithRed:(200.0/255.0) green:(200.0 / 255.0) blue:(250.0 / 255.0) alpha:1];
<5-3>给Navigation Bar设置背景图片
(1)iOS 5.0
在viewdidLoad中增加:
if ([self.navgationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
UIImage *image= [UIImage imageNamed:@"navBar_bg.png"];
CGSize navBarSize = self.navigationController.navigationBar.bounds.size;
image = [self scaleImage:image ToSize:navBarSize]; ///<5-4>方法
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
(2)低于iOS5.0版本
自定义navigationBar,设置背景方法:
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"navBar_bg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
<5-4>缩放图片到制定的大小
+(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)size{
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
<5-5>使用Push和Pop打开和关闭ViewController
[self.navigationController pushViewController:viewController animated:YES];
[self.navigationController popViewControllerAnimated:YES];