Navigation Bar的背景图片设置

转自:http://my.oschina.net/plumsoft/blog/55927


环境:xcode5.0+OS10.8.4


只需在.M文件。。。的viewDidload方法中最后添加代码:

单色:

//设置Navigation Bar颜色

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(218.0/255.0) green:(228.0 / 255.0) blue:(250.0 / 255.0) alpha:1];
背景(需去掉以上代码):

//设置Navigation Bar背景图片
UIImage *title_bg = [UIImage imageNamed:@"title_bg.png"];  //获取图片
CGSize titleSize = self.navigationController.navigationBar.bounds.size;  //获取Navigation Bar的位置和大小
title_bg = [self scaleToSize:title_bg size:titleSize];//设置图片的大小与Navigation Bar相同
[self.navigationController.navigationBar 
     setBackgroundImage:title_bg
     forBarMetrics:UIBarMetricsDefault];  //设置背景
同时加上以下代码,否则出错:

//调整图片大小
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}


你可能感兴趣的:(图片,Navigation)