Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 设置导航栏背景图像后,“自定义后退”按钮消失_Iphone_Image_Invisible_Navigationbar - Fatal编程技术网

Iphone 设置导航栏背景图像后,“自定义后退”按钮消失

Iphone 设置导航栏背景图像后,“自定义后退”按钮消失,iphone,image,invisible,navigationbar,Iphone,Image,Invisible,Navigationbar,我正在使用以下类别代码进行更改 导航栏的背景图像 #import <Foundation/Foundation.h> @interface UINavigationBar (CustomImage) - (void) setBackgroundImage:(UIImage*)image; - (void) clearBackgroundImage; @end #import "UINavigationBar+CustomImage.h" @implementation UIN

我正在使用以下类别代码进行更改 导航栏的背景图像

#import <Foundation/Foundation.h>

@interface UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image;
- (void) clearBackgroundImage;
@end


#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image {
    if (image == NULL) return;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self insertSubview:imageView atIndex:0];
    [imageView release];
}

- (void) clearBackgroundImage {    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSArray *mySubviews = [self subviews];

    for (int i = [mySubviews count] - 1; i >= 0; i--)
    {
        if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
        {
            [[mySubviews objectAtIndex:i] removeFromSuperview];
            return;
        }
    }

    [pool release];
}
@end
但是按钮大部分时间都隐藏在bg图像下 有时它会随机出现。 为什么会这样?我不确定图像插入索引0时有什么问题 据我所知,它应该一直落后。
请提供帮助。

您应该在添加图像后尝试创建按钮。

在加载导航栏的类中尝试以下代码以更改导航栏的背景

@interface UINavigationBar (MyCustomNavBar)
@end
@implementation UINavigationBar (MyCustomNavBar)
- (void) drawRect:(CGRect)rect 
{
    UIImage *barImage = [UIImage imageNamed:@"image.png" ];
    [barImage drawInRect:rect];
}
@end

使用Praveen S建议的方法,您可以为下一个背景图像名称设置一个全局变量,并在viewDidLoad方法中进行设置,然后在draw rect方法中使用存储在变量中的imageName,而不是在draw rect中对其进行硬编码。

但这就是我正在做的[[dataCenter.navController navigationBar]performSelectorInBackground:@selector(setBackgroundImage:)with object:[UIImage ImageName:@“headerlogo.png”];然后添加按钮我以前使用过这段代码,但是图像应该在视图之间更改,我可以使用此函数吗?对于视图之间,您可以保留一个值来跟踪它。我将代码放在AppDelegate的.m文件中,并使用一个全局变量,我使用以下代码调用函数:[[center.navController navigationBar]performSelectorInBackground:@selector(drawRect:)with object:nil];但是图像没有改变,我应该先删除前一个吗?放在你创建导航控制器的位置我不确定你的意思是什么?我正在appDelegate中创建导航
@interface UINavigationBar (MyCustomNavBar)
@end
@implementation UINavigationBar (MyCustomNavBar)
- (void) drawRect:(CGRect)rect 
{
    UIImage *barImage = [UIImage imageNamed:@"image.png" ];
    [barImage drawInRect:rect];
}
@end