图像未在导航控制器中正确设置

时间:2012-11-30 10:28:41

标签: ios xcode

我写了以下代码......

enter image description here

      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]; 

     }

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

提前感谢。

1 个答案:

答案 0 :(得分:1)

这是您将图像设置到导航栏的方式

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

并将此代码放在以下方法中

- (void)viewWillAppear:(BOOL)animated

将BarMetrics设置为默认值

 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];