导航控制器无法正常工作

时间:2012-05-08 13:08:52

标签: iphone xcode uinavigationbar

我在我当前的应用程序中使用导航控制器,但是导航控制器与iOS4和iOS5有问题所以我试着编写iOS 4和iOS 4的代码。 5

if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
else
{
    self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"header.png"].CGImage;
}

但问题是当我在iOS 4版本上运行我的应用程序时,我的导航控制器看起来像这样。enter image description here

请建议我。

2 个答案:

答案 0 :(得分:2)

感谢经过长时间的搜索,我尝试了这个帮助我的代码。

导入“ImageViewController.h”

@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"background.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end
implement this code in your .m file


@implementation ImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    [self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
    [backGroundView release];
}

@end

答案 1 :(得分:0)

UINavigationController *navControl;

在其他方面,请尝试这样。

UINavigationBar *navBar = self.navControl.navigationBar;
   UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
            imgView.image = [UIImage imageNamed:@"header.png"];
            [navBar addSubview:imgView];
            [imgView release];