2

我写了以下代码...

在此处输入图像描述

      if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {

    //iOS 5
    UIImage *toolBarIMG = [UIImage imageNamed: @"header.png"];  

    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
        [self.navigationController.navigationBar setBackgroundImage:toolBarIMG  
                                                      forBarMetrics:0]; 
    } else {

        //iOS 4
        [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header.png"]] 
                                                               atIndex:0]; 

     }

图像显示在导航控制器中,但图像的开始和结束存在一些像素间隙,显示黑色图像。如何设置图像以消除黑色。你可以看到粉红色的圆形,我想去除那些黑色..

提前致谢。

4

1 回答 1

1

This is how you set an image to the navigation bar

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"header.png"];
[navBar setBackgroundImage:image];

And put this code in the below method

- (void)viewWillAppear:(BOOL)animated

Set Your BarMetrics to Default

 if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {

//iOS 5
UIImage *toolBarIMG = [UIImage imageNamed: @"header.png"];  

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
    [self.navigationController.navigationBar setBackgroundImage:toolBarIMG  
                                                  forBarMetrics:UIBarMetricsDefault]; 
} else {

    //iOS 4
    [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header.png"]] 
                                                           atIndex:0]; 
于 2012-11-30T10:33:09.860 回答